- Home /
Unexpected problems with moving objects with a mouse click.
Hello everyone, I have a script that moves an object to a specific position by clicking on it, but on the first and second mouse clicks, the objects move to the same position but must be in different positions. Here is the code, first part where I determine where the object should go:
void Update () {
Debug.Log(NumPos);
if (Input.GetMouseButtonDown(0))
{
Vector2 origin = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f);
if (hit.transform.gameObject.tag == "Ball") {
if(goFirstPos == false)
{
NumPos = 0;
goFirstPos = true;
}
else
{
if(goSecondPos == false )
{
NumPos =1;
goSecondPos = true;
}
else
{
if(goThirdPos == false)
{
NumPos = 2;
goThirdPos = true;
}
else
{
if(goForthPos == false)
{
NumPos = 3;
goForthPos = true;
} else
{
if(goFivePos == false)
{
NumPos = 4;
goFivePos = true;
} else
{
if(goSixPos == false)
{
NumPos = 5;
goSixPos = true;
}
}
}
}
}
}
}
}
}
and the second where the movement is applied:
private bool ballHitted = false;
BallHit ballHit;
int i = 7;
// Start is called before the first frame update
void Start()
{
ballHit = FindObjectOfType<BallHit>();
}
// Update is called once per frame
void Update()
{
if(ballHitted)
transform.position = Vector2.MoveTowards(transform.position, tentacle[i].transform.position, speed * Time.deltaTime);
/* RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.transform.name== "ball")
{
Debug.Log("sdds");
} */
}
private void OnMouseDown() {
i = ballHit.NumPos;
ballHitted = true;
}
By the way, these are two different scripts, the second one is attached to an object
Answer by highpockets · Nov 29, 2020 at 12:20 AM
Ok there seems to be a lot going on here that could be simplified, but I think your main issue is that you are depending on a OnMouseDown() in the second script and Input.GetMouseButtonDown() on the first. I’m unsure when OnMouseDown() is called, but if it is before Update(), you will not get the changes in your second script, so my thought is that it is an execution order issue. I think your first script should fire an event that the second one listens to and your first script doesn’t seem to need all those Booleans. Consider the following:
using UnityEngine.Events; // make sure to include events
[System.Serializable]
public class MyIntEvent : UnityEvent<int> {}
public class BallHit : MonoBehaviour
{
public MyIntEvent posChangeEvent;
private int NumPos = 0;
void Start()
{
if(posChangeEvent == null)
posChangeEvent = new MyIntEvent();
}
void Update () {
Debug.Log(NumPos);
if (Input.GetMouseButtonDown(0))
{
Vector2 origin = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f);
if (hit.transform.gameObject.tag == "Ball") {
NumPos++;
posChangeEvent.Invoke(NumPos);
}
}
}
}
This just adds 1 to NumPos and fires an event with the NumPos. Now just listen for this in your second script. private bool ballHitted = false; BallHit ballHit; int i = 7;
// Start is called before the first frame update
void Start()
{
ballHit = FindObjectOfType<BallHit>();
ballHit.posChangeEvent.AddListener(PosChange);
}
// Update is called once per frame
void Update()
{
if(ballHitted)
transform.position = Vector2.MoveTowards(transform.position, tentacle[i].transform.position, speed * Time.deltaTime);
}
private void PosChange(int num) {
i = num;
ballHitted = true;
}
void OnDestroy()
{
ballHit.posChangeEvent.RemoveListener(PosChange); //avoid memory leaks by removing the event listener.
}
Thank you very much, events are what I needed :) But I still think that I need boolean values, because I planned that the player can destroy 2 identical objects, and the new objects should know where the free position without the object is and move there.