I want to teleport my player to a random(from these 4) spot on the x axis, but it doesn't work.
private float[] x = {-8, -4, 4, 8};
private float y = 1;
private float z = 0;
Vector3 NewPos;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Door"))
{
Vector3 tempPos = transform.position;
int mynum = Random.Range(0, x.Length);
NewPos = new Vector3(x[mynum], y, z);
tempPos.x = NewPos.x;
tempPos.y = NewPos.y;
tempPos.z = NewPos.z;
}
Comment
Answer by MT369MT · Sep 19, 2018 at 08:10 PM
Hi, you only stored the value in a variable but that doesn’t affect your player position. Use this instead:
int mynum = Random.Range(0, x.Length);
NewPos = new Vector3(x[mynum], y, z);
transform.position = NewPos;
Your answer
Follow this Question
Related Questions
Vector2.MoveTowards Not Working 1 Answer
Translating an Object to a Random Endpoint,Moving an Object to a random endpoint 0 Answers
Detect overlapping objects 2D game 0 Answers
If Statement not working properly - Comparing game objects 0 Answers
Why does my random colour generator always spit out the same colour? 0 Answers