- Home /
Co ordinates issue
I've set my co ordinates in my code for a unity 2d game to move the tile to a set position. However it never goes to the X or y I have defined in inspector leaving me a bit confused to what's wrong is it a simple fix or resolution issue. I do not have z defined also as I don't see the point
Yeah, without the code you use to move your character, couldn't begin to help you; please provide complete script that handles the movement.
using System.Collections;
using System.Collections.Generic
using Unity Engine;
public class RandomPosition : $$anonymous$$onoBehaviour
{
public Vector3] positions;
public void SetRandomPosition)
{
int random Number = Random.Range(0,
positions.Length);
transform.position positions [random Number];
}
}
This is a 2d game btw
Your code has countless of typing errors so it's impossible to find any actual error here. This code will not compile. Also we don't know if and where you might call that SetRandomPosition function. Provide your actual code. Also what did you already do to debug your issue? Have you checked that your code actually executes? Try adding a Debug.Log to see if the code actually runs.
Answer by I_Am_Err00r · Jul 12, 2019 at 12:44 PM
You defined positions as a Vector3, not an int. I hope this analogy doesn't offend you but you are trying to compare things that don't compare with that statement like this pen is a car. Try this:
using System.Collections;
using System.Collections.Generic
using Unity Engine;
public class RandomPosition : MonoBehaviour
{
float randomRangeX;
float randomRangeY;
public int randomRange;
public Vector3 positions;
public void SetRandomPosition)
{
randomRangeX = Random.Range(0,
randomRange);
randomRangeY = Random.Range(0,
randomRange);
positions = new Vector3 (randomRangeX, randomRangeY, 0);
transform.position = positions;
}
}
What I am doing here is assigning the values of x and y to floats (ints with decimal values) and then when you call the function, getting those random numbers generated. After you have those, when you define the Vector3 positions, you plug in the random x and y values you just created (and left z at 0 because you don't care about that in 2D). JUST A REMINDER, you need to set the value randomRange in the inspector for this to work.
Let me know if that works.
Sorry but your code also makes not much sense just as the OP's code in the comment above. This won't compile. Also he set a list of positions in the inspector from which he want to choose from.
You're right, my code didn't compile (wrote it directly on this forum, not an IDE, didn't notice the errors I made when I originally wrote that, fixed it) but someone of your reputation should understand where I was going with that and where the OP is going as well, no need to be so abrasive.
Here is what I think OP is trying to do now that I gave this a second look:
using System.Collections;
using System.Collections.Generic
using Unity Engine;
public class RandomPosition : $$anonymous$$onoBehaviour
{
public Vector3[] positions;
public void SetRandomPosition
{
int random Number = $$anonymous$$athf.RoundToInt(Random.Range(0,
positions.Length)); // I think that is how you round a float to an int, I'm not using an IDE so I double check if you copy and past this
transform.position = positions[random Number];
}
}
It gives random positions but I can't seem to customize where it goes so sometimes it goes a little of. Is there a way where I can say it can go from x100 y0 to x500 y100 so it can move anywhere out of them co ordinates
Answer by robbos729 · Jul 13, 2019 at 06:09 PM
For some reason it still goes to some which aren't desired is It the vector 3
using System.Collections;
using System.Collections.Generic;
using Unity Engine;
public class RandomPosition : MonoBehaviour
{
public Vector3[] positions;
public void SetRandomPosition()
{
int randomNumber = Mathf.RoundToInt(Random.Range(0,
positions.Length)); // I think that is how you round a float to an int, I'm not using an IDE so I double check if you copy and past this
transform.position = positions[randomNumber];
}
}