How could I set this up so that it adds the force relative to the camera’s direction instead of world? Currently have this attached to a ball object and slightly modified orbit script attached to camera.
function FixedUpdate () {
var dir : Vector3 = Vector3.zero;
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
rigidbody.AddForce(dir * force);
}
I was thinking something like below, but that doesn’t seem to work.
function FixedUpdate () {
var camdir : Vector3 = camera.forward;
var dir : Vector3 = Vector3.zero;
dir.x = Input.GetAxis("Horizontal");
dir.z = Input.GetAxis("Vertical");
var relativedir : Vector3 = Vector3.Cross(camdir, dir);
rigidbody.AddForce(relativedir * force);
}
Edit: came up with this, it partially works. The only problem is that it doesn’t seem to like diagonal directions. If I rotate the camera 30 degrees to left and push forward, it will go towards 0 degrees. If I rotate camera 60 degrees and push forward, it goes towards 90 degrees.
function Update() {
var camDir : Vector3 = Camera.main.transform.forward;
var inputDir : Vector3 = Vector3.zero;
inputDir.x = Input.GetAxis("Left_Horizontal");
inputDir.z = Input.GetAxis("Left_Vertical");
var dir : Vector3 = Vector3.zero;
dir.x = camDir.x * inputDir.x;
dir.z = camDir.z * inputDir.z;
rigidbody.AddForce(dir * force);
}