- Home /
Raycast moves away from object when object is rotated.
i have a game object with a script attached. I am casting a series of rays around the object. The problem is that when i move the object away from world origin and rotate it this causes the ray direction to move in the opposite direction. The problem doesn't occur if I move the object or rotate the object. It only happens when i do both. Here's the code. Thanks in advance.
for (int i = 0; i < NumOfRays; i++) {
x = Mathf.Sin (angle);
z = Mathf.Cos (angle);
angle += 2 * Mathf.PI / NumOfRays;
Vector3 dir = transform.TransformDirection( new Vector3 (transform.position.x + x, 0, transform.position.z + z));
Debug.DrawLine (transform.position, dir, Color.red);
if (Input.GetKeyDown(KeyCode.UpArrow))
rb.AddForce (Vector3.forward);
}
angle = 0;
}
Answer by ElijahShadbolt · Dec 23, 2017 at 04:09 AM
The line should be
Vector3 dir = transform.TransformDirection(new Vector3(x, 0, z));
because TransformDirection takes in a local space direction and outputs a world space direction (where the vector has been rotated by transform.rotation).
Also use Debug.DrawRay instead of DrawLine.
Thank you, Stupid me didn't read your response properly. so i spent he last hour remaining confused. Thanks again.
Happy to help! Don't call yourself stupid or idiot just because you don't know something. You are making an active effort to learn new things and increase your knowledge! $$anonymous$$eep up the good curiosity!
Your answer
Follow this Question
Related Questions
My rotate dont work, using raycast, C# 1 Answer
Move object in an arc 3 Answers
How to get a ai to drive to a raycast 0 Answers
After collision rotate and move 1 Answer
Why is this Raycast not working ? 3 Answers