- Home /
Move Rigidbody Relative To Camera, quick Q (hopefuly)
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
Answer by robertbu · Oct 11, 2013 at 04:48 PM
In reading through, I'm assuming you have a 2D, axes aligned game. You say, " I only want it adding force in a horizontal world axis," but since you implement Up/Down as well as Left/Right. I assume you mean horizontal and vertical axes.
If this is the case, the simplest solution is just to remove any 'Z' component from the force vector:
var camDir = Camera.main.transform.TransformDirection(Vector3.forward);
camDir.z = 0.0;
rigidbody.AddForce(camDir.normalized * speed * Time.deltaTime);
You may also want to freeze 'z' movement in your Rigidbody.
No but your answer did answer it! What I needed was camDir.y = 0.0 for every direction. Now it adds force to both X and Z relative to camera, without adding force on Y, if I understood it right.
I want a kind of free/orbiting cam where the controls depend on its direction, so arrow key up will always move the character forward relative to camera and left will always move the character to the left of the screen relative to camera and so on. I'm not sure but I think Zelda has that kind of 3rd person camera? Never played it my self but from what I've seen while googling thats the thing I want.
Now the next thing I want is for the player to rotate to the direction its moving but that's for another question later after I run out of ideas how to do it, since I don't wanna come here asking for done scripts. xD
Thank you robertbu, somehow I had a feeling you'd be the one to answer since from my exp searching through unity answers yours are amongst the ones I noticed most helpful. xD
Your answer
Follow this Question
Related Questions
Rotate object ON THE CAMERA UP AXIS 2 Answers
Typecasting Camera to Object for ObjectField 3 Answers
How to make your character move? 7 Answers
Move another object 1 Answer
How can I change camera when colliding with "X" object? 1 Answer