Ok so I’ve been at this the entire day cause I’m feeling so out of the zone, even tho I should be working on a trailer for my other project which is complete.
So I cooked up this simple script to move the rigidbody relative to the orbiting camera. So when you press left it goes left relative to the camera, meaning always going to the left of the screen no matter the cam rotation.
However since camera isn’t perfectly behind the player, its usually above a bit, when moving in any direction, lets say forward in this example, the force is pushing player forward and down since the camera is pointing down a bit too.
So how do I limit it to only add force on the Y axis of 0, or assume that the camera’s Vector3 is (0,0,1) or I don’t even know how to phrase the question. xD
Here’s the entire player controller code, and the camera is the MouseOrbit script from Unify wiki:
var speed : float = 10;
var jump : float = 10;
var inAir : boolean = true;
function Update()
{
if (Input.GetButton("Up"))
{
var camDir = Camera.main.transform.TransformDirection(Vector3.forward);
rigidbody.AddForce(camDir * speed * Time.deltaTime);
}
else if (Input.GetButton("Down"))
{
var camDir2 = Camera.main.transform.TransformDirection(Vector3.back);
rigidbody.AddForce(camDir2 * speed * Time.deltaTime);
}
if (Input.GetButton("Left"))
{
var camDir3 = Camera.main.transform.TransformDirection(Vector3.left);
rigidbody.AddForce(camDir3 * speed * Time.deltaTime);
}
else if (Input.GetButton("Right"))
{
var camDir4 = Camera.main.transform.TransformDirection(Vector3.right);
rigidbody.AddForce(camDir4 * speed * Time.deltaTime);
}
if(Input.GetButton("Jump") && inAir == false)
{
inAir = true;
rigidbody.AddForce(Vector3(0,1,0) * jump * Time.deltaTime);
}
}
function OnCollisionStay()
{
inAir = false;
}
function OnCollisionExit()
{
inAir = true;
}
EDIT: The entire code works flawlessly, except I only want it adding force in a horizontal world axis relative to the camera. Hope someone understand what i mean. xD