Rotating Vector3, linecast
Hello, I am am creating a Physics2d.LineCast that starts in middle of the objects, and ends in same vector with x bigger by 1 unit. It looks like that:
I am trying to make this linecast follow transforms rotation, so for example, if I would rotate my transform by 90 degrees on Z axis, that would be my expectation (left) but instead I get linecast with weird rotation plus linecast is much longer (depends on transforms rotation)
I am drawing linecast and rotating it this way:
void FixedUpdate ()
{
Vector3 myVector = transform.position + new Vector3(1, 0, 0);
Vector3 rotatedVector = transform.rotation * myVector;
Debug.DrawLine(transform.position, rotatedVector);
}
But as you can see it does not work as intended, I would appreciate help how to rotate Vector3 properly, so it follows transforms rotation. Thank you.
Answer by Hellium · Jan 31, 2017 at 02:35 PM
In Vector Arithmetic, a sum of two vectors results in a point, not a vector while the difference between two points results in a vector. Here, you need to rotate a directional vector, thus, simply do the following :
void FixedUpdate()
{
Vector3 rotatedVector = transform.rotation * new Vector3(1, 0, 0);
Debug.DrawLine( transform.position, transform.position + rotatedVector );
}
Thank you very much, indeed I did not think about it this way, once again thanks.
Your answer
Follow this Question
Related Questions
unwanted rotation in y axis 0 Answers
limit Quaternion.Euler rotation 2 Answers
How can I set rotation properly? 0 Answers
[Solved] Quaternion from IMU sensor to GameObject Orientation problem 3 Answers
Clamping rotation between two angles 0 Answers