- Home /
if position not working
Hi, I have a problem with checking if my gameobject is on a position, let me explain: I'm making a game with a kind of "Slider", like the Unity UI Slider, (But not using the slider, it's just to compare), this Slider was made with GameObjects. The cursor was designed to move automatically and you press the screen when the cursor is in a specific zone. This is the cursor code:
private System.Random FirstRnd = new System.Random();
private int FirstCounter;
private float Speed = 50f;
private float AntiSpeed = -50f;
private int SecondCounter;
private System.Random SecondRnd = new System.Random();
private Vector3 StartLVector = new Vector3(-2.05f, -0.55f, 10f);
private Vector3 StartRVector = new Vector2(2.05f, -0.55f);
private Vector3 CenterL = new Vector3(-0.02f, 0.55f, 10f);
private Vector3 CenterR = new Vector3(1.14f, -0.55f, 10f);
private GameObject Cursor;
// Use this for initialization
void Start ()
{
Cursor = GameObject.Find("Cursor");
}
private void FixedUpdate()
{
SecondStarting();
}
void Starting()
{
FirstCounter = FirstRnd.Next(1, 3);
if (FirstCounter == 1)
{
transform.position = StartLVector;
}
else if (FirstCounter == 2)
{
transform.position = StartRVector;
}
}
void SecondStarting()
{
Debug.Log("Puga");
if (Cursor.transform.position == StartLVector)
{
Cursor.transform.Translate(Speed, 0, 0);
}
else if (Cursor.transform.position == StartRVector)
{
Cursor.transform.Translate(AntiSpeed, 0, 0);
}
else if (Cursor.transform.position == CenterL)
{
SecondCounter = SecondRnd.Next(1, 3);
if (SecondCounter == 1)
{
Cursor.transform.Translate(Speed, 0, 0);
}
else if (SecondCounter == 2)
{
Cursor.transform.Translate(AntiSpeed, 0, 0);
}
}
else if (Cursor.transform.position == CenterR)
{
SecondCounter = SecondRnd.Next(1, 3);
if (SecondCounter == 1)
{
Cursor.transform.Translate(Speed, 0, 0);
}
else if (SecondCounter == 2)
{
Cursor.transform.Translate(AntiSpeed, 0, 0);
}
}
}
}
As far I can see, I don't detect a problem here. Do you see something?
I want to add something, the message "Puga" yes prints, but, below it, nothing seems to compile
Have you added debug logs to each of your loops to see what’s getting called when? Good place to start.. does it move at all? What happens? Offhand, you tell the object to translate if it has a certain position, that will only happen for one frame before it is no longer true.. also, you repeat a block of code, that should be a separate method you call twice for more manageable code.. overall, I still can’t quite put together how this is meant to work though..
Answer by LCStark · Sep 22, 2018 at 07:58 AM
You are comparing Cursor.transform.position
to some values and none of them match. Try displaying the Cursor.transform.position
in the Debug.Log
and see what value it really has. You probably will want to use transform.localPosition
instead.