- Home /
Random.insideUnitCircle seems to reproduce similar vectors
I am using Random.insideUnitCircle to calculate a random vector in which for my enemy to look when searching for the player. The enemy will stare in this direction for a few seconds, then a new vector will be calculated and he will turn his head.
These are the variables I set whenever the new vector needs to be calculated:
endRot = Random.insideUnitCircle.normalized; step = lookSpeed * Time.deltaTime;
Here is my Update():
transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, endRot - (Vector2)transform.position, step, 0f)); Debug.DrawRay(transform.position, endRot - (Vector2)transform.position, Color.red);
The issue is that the vectors are all extremely close to each other and so the enemy doesn't seem to be looking around. The Debug.DrawRay() confirms that the directions are within a few degrees of each other, even if I recalculate the vectors multiple times and have the enemy look around a lot.
I have also tried changing the Random.InitState() before using Random.insideUnitCircle. If I need to post all the code let me know.
Answer by Bunny83 · May 22, 2020 at 12:27 AM
Uhm your "endRot" vector is a direction vector. Why do you subtract the object's position from that vector? That would always give you a vector that points roughly towards the world origin. Just remove your - (Vector2)transform.position
completely:
transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, endRot, step, 0f));
Debug.DrawRay(transform.position, endRot, Color.red);
I'm totally kicking myself right now. The real issue here was me simply not understanding LookRotation(). Thanks for helping me out!