- Home /
Rotating a direction Vector3 by Quaternion
I've done research but every solution I've found gives me the same result when trying to just move forward in relation to the camera. C#
[RequireComponent(typeof(Rigidbody))]
public class RollerController : MonoBehaviour {
public float BasicSpeed = 2f;
public float BoostAmount = 10f;
CameraController playerCamera;
// Use this for initialization
void Start () {
playerCamera = GetComponent<CameraController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
rigidbody.AddForce((camera.transform.rotation * playerCamera.transform.forward) * BasicSpeed);
}
this code seems like it should work right? My game object just moves in circles when I hold the w key down.. What is going on?!?
edit:: sorry been long day it actually just doesn't rotate at and continues along the forward direction?
edit:: Thought maybe my camera controller was messing with the rotation of the camera when passing the values?? just a thought here's the camera controller I am using. Its not complete was just getting it working first.
public class CameraController : MonoBehaviour {
public Transform CameraTransform;
public float DistanceUp = 10;
public float DistanceBack = 5;
public float RotationSpeed = 3;
public float total = 0;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
total += Input.GetAxis("Mouse X") * RotationSpeed;
CameraTransform.rotation = Quaternion.Euler(new Vector3(40, 0, 13));
CameraTransform.position = transform.position + new Vector3(0, DistanceUp, -DistanceBack);
CameraTransform.RotateAround(transform.position, Vector3.up, total);
}
}
Answer by joemane22 · Dec 21, 2014 at 12:58 PM
The solution to this is to use the Unity Matrix4x4 struct. You can easily rotate a vector point after creating the appropriate Matrix by calling Matrix4x4.TRS(Vector3 translation, Vector3 rotation,Vector3 scale). If you pass a zero vector into translation and a one vector into scale and just set the rotation to the desired rotations like this
Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(new Vector3(xRotation, yRotation, zRotation)), Vector3.one);
You can easily rotate a direction Vector by calling
matrix.MultiplyPoint(directionVector);
Hope this helps anyone that may run into this same problem!
Answer by tanoshimi · Dec 21, 2014 at 10:32 AM
Don't you just want camera.transform.forward ?
this is just a simple version actually I want to isolate the Y rotation since the camera is looking down on the object a bit. This version is here because I have tried every possible way I could think of just to get it to work slightly and build on that.
Actually just tried Camera.transform.forward for the sake of it and it is the version that goes in circles.. strange
I honestly don't understand this.. I have made 100s of cameras in xna using matrices and never had a problem. I know how cameras work and how to get them to move and the player to.. I think I am just going to do the math the hard way... using Vector math.. seems easier