- Home /
Question by
TheRandomPerson6 · Mar 13 at 02:44 PM ·
directionraycasts
Raycasts not changing direction when using transform.lookAt()
Hi, so Im changing enemys direction with transform.lookat, and I use raycasts to see if player hit raycast. However when for some reason raycasts direction not changes when I do transform.lookAt();
Code:
//Creates Raycasts
Ray see1 = new Ray();
Ray see2 = new Ray();
Ray see3 = new Ray();
//Raycasts position
see1.origin = new Vector3(transform.position.x - 0.1f, transform.position.y, transform.position.z);
see2.origin = transform.position;
see3.origin = new Vector3(transform.position.x + 0.1f, transform.position.y, transform.position.z);
//Raycasts direction
see1.direction = Vector3.forward;
see2.direction = Vector3.forward;
see3.direction = Vector3.forward;
//For Debug
Debug.DrawRay(see2.origin, see2.direction, new Color(255, 0, 0), 1000000f);
if (states == theState.patrol)
{
agent.destination = randomizeA;
transform.LookAt(new Vector3(randomizeA.x, transform.position.y, randomizeA.z));
}
Comment
Best Answer
Answer by asafsitner · Mar 13 at 02:52 PM
Your problem is probably with the declaration of the raycast direction: you're using Vector3.forward
which is in global space, while you want to shoot while taking the object's own rotation into account, which means local space.
What you want to do is set the raycast direction to the transform.forward
value, which is the direction the object itself is facing after calculating its (and its parents's) rotations, such as after your .LookAt()
call.
Your answer
