Rotate a vector based on a direction (transform.up) of a game object
I want to shoot two vectors down a object one with a angle of 135 degrees and the other with a angle of -135 degrees with the up vector of the game object. But I am having some issues to keep the vectors at the right position when the rotation of the object changes. See the picture below for a example. The blue line is the up vector.
When I change for example the X rotation of the object the two vectors will rotate towards the screen. But I want them to stay at the same position relative to the game object. Which in this case is pointing away from the camera and through the bottom plane of the cube.
Here follows a simplified version of the code I use. In my code I don't have access to the transform of the object only the rotation (quaternion) so I cant use method from transform.xxx
public class RotationTest : MonoBehaviour {
Vector3 minDir;
Vector3 masDir;
// Update is called once per frame
void Update () {
minDir = (Quaternion.Euler(0, 0, -135)) * transform.up;
masDir = (Quaternion.Euler(0, 0, 135)) * transform.up;
//baseMinDir = Quaternion.AngleAxis(-135, transform.up) * Vector3.up;
//baseMaxDir = Quaternion.AngleAxis(135, transform.up) * Vector3.up;
Debug.DrawRay(transform.position, transform.up, Color.cyan);
Debug.DrawRay(transform.position, minDir, Color.yellow);
Debug.DrawRay(transform.position, masDir, Color.yellow);
}
}
Answer by Munchy2007 · Dec 04, 2015 at 11:43 AM
@Janjanus This seems to work
using UnityEngine; using System.Collections;
public class RotationTest : MonoBehaviour {
Vector3 minDir;
Vector3 masDir;
// Update is called once per frame
void Update () {
minDir = transform.up;
Quaternion quat = Quaternion.AngleAxis(135,transform.forward);
minDir = quat * minDir;
masDir = transform.up;
quat = Quaternion.AngleAxis(-135,transform.forward);
masDir = quat * masDir;
Debug.DrawRay(transform.position, transform.up, Color.cyan);
Debug.DrawRay(transform.position, minDir, Color.yellow);
Debug.DrawRay(transform.position, masDir, Color.yellow);
}