Home>
We are making a puzzle game that fits shapes in unity.
When the shape fits the shape, I wrote the script using the bool value so that it could not be moved, but it does not change even if the condition is satisfied.
Below script
Script with method written
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMethodScript: MonoBehaviour
{
public int goalcount;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log(goalcount);
}
public void grphicmethod(string name1, string name2, float buzzle, Vector3 point, bool fit)
{
if (Input.GetMouseButton(0))
{
point = Camera.main.WorldToScreenPoint(transform.position);
Ray ray = new Ray();
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction, Mathf.Infinity);
if (hit.collider.gameObject.CompareTag(name1))
{
point = Camera.main.WorldToScreenPoint(transform.position);
Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, point.z);
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint);
hit.collider.gameObject.transform.position = currentPosition;
}
}
if (GameObject.FindWithTag(name1).transform.position.x >GameObject.FindWithTag(name2).transform.position.x-buzzle
&&gameObject.FindWithTag(name1).transform.position.x<GameObject.FindWithTag(name2).transform.position.x + buzzle
&&gameObject.FindWithTag(name1).transform.position.y >GameObject.FindWithTag(name2).transform.position.y-buzzle
&&gameObject.FindWithTag(name1).transform.position.y<GameObject.FindWithTag(name2).transform.position.y + buzzle)
{
fit = false;
Debug.Log("OK");GameObject.FindWithTag(name1).transform.position = GameObject.FindWithTag(name2).transform.position;
}
}
}
Script that runs the method
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
public class GameControllerScript: MonoBehaviour
{
public Vector3 screenPoint;
public GameObject tr, cir, squ, gen, st, trgoal, cirgoal, squgoal, gengoal, stgoal;
private int a, b, c;
public bool trfit, genfit, cirfit, squfit, stfit;
public GameObject[] grphics;
public GameObject[] goal;
private GameMethodScript gms;
// Start is called before the first frame update
void Start()
{
grphics = new GameObject[]{ tr, cir, squ, gen, st };
goal = new GameObject[] {trgoal, cirgoal, squgoal, gengoal, stgoal };
gamestart();
gms = GameObject
.FindWithTag("GameMethod")
.GetComponent<GameMethodScript>();
}
void Update()
{
Debug.Log(trfit);
if (trfit)
{
gms.grphicmethod("tr", "trgoal", 0.2f, screenPoint, trfit);
}
gms.grphicmethod("gen", "gengoal", 0.2f, screenPoint,genfit);
gms.grphicmethod("cir", "cirgoal", 0.2f, screenPoint,cirfit);
gms.grphicmethod("squ", "squgoal", 0.2f, screenPoint, squfit);
gms.grphicmethod("st", "stgoal", 0.2f, screenPoint,stfit);
}
public void gamestart()
{trfit = true;
genfit = true;
cirfit = true;
squfit = true;
stfit = true;
a = Random.Range(0, 5);
do
{
b = Random.Range(0, 5);
}
while (a == b);
do
{
c = Random.Range(0, 5);
}
while (a == c || b == c);
GameObject[] goal2 = {goal[a],
goal[b],
goal[c] };
goal2 = goal2.OrderBy(i =>System.Guid.NewGuid()).ToArray();
Instantiate(grphics[a],
new Vector3(-5f, -2.5f, 0f), transform.rotation);
Instantiate(grphics[b],
new Vector3(0f, -2.5f, 0f), transform.rotation);
Instantiate(grphics[c],
new Vector3(5f, -2.5f, 0f), transform.rotation);
Instantiate(goal2[0],
new Vector3(-5f, 2f, 0f), transform.rotation);
Instantiate(goal2[1],
new Vector3(0f, 2f, 0f), transform.rotation);
Instantiate(goal2[2],
new Vector3(5f, 2f, 0f), transform.rotation);
}
}
In the method
DebugDebug.Log("OK");
Is moving.
Please tell me.
-
Answer # 1
Related articles
- [java] about method call when subclass instance is created with super class type
- c ++ - about the return value of the constructor
- java - about the minus of the method
- c # - [wpf] how to put a changing value in the binding path
- about the value of "i" in python, for i in range (len (variable)):
- go - for c: = range ch {} about the method of close (ch) when sending a value to ch in the clause
- python - django formset value method
- python - about the feature selection method
- please tell me about the extraction method when the parentheses of the json part are nested in python3 scraping
- [java] about equals method (reason why nullpointerexception does not occur even if null is passed/override)
- python - about image processing of hog method
- programming language - about method argument types
- c ++ - questions about passing by value and passing by reference
- python - about the target method when nothing is returned for the query of table
- java print method? about
- python - about the return value of the assertis method
- python 3x - [python] about the method of totaling for each data frame and element
- java - about the get method
- java - about method overloading
- c # i want to pass a value to another method in the same class
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
It was solved by adding "out".