- Home /
Move RigidBody character relative to camera.
I know there are a million other questions like this here, so sorry for being an idiot. Here goes:
My character is a RigidBody ball, and I have this:
( "a < b" would mean that a is the parent of b)
Ball < GameObject < Camera
Ball has a simple movement script attached.
GameObject has a script to stop it from rotating when Ball does.
Camera has a camera movement script.
Everything works right, except that the movement of Ball is not camera relative. Pushing forward always results in movement in one direction, regardless of which way the camera points.
The complicating factor is that I can't simply make the y rotation of Ball equal to that of the Camera, because that would effect the physics (if you hit a wall while spinning the camera you would bounce off differently).
So how do I make movement of my character relative to camera direction without rotating my character on the y axis when the camera turns?
JavaScript please!!!!
My movement script:
var speed : float = 6.0; private var RotDirection : Vector3 = Vector3.zero;
function FixedUpdate () { RotDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); RotDirection *= speed;
rigidbody.AddTorque (RotDirection);
}
Answer by The_r0nin · Dec 19, 2010 at 05:58 PM
impulseDirection:Vector3 = Camera.main.transform.TransformDirection(Vector3(0,0,10));
That should give you a world-vector for your local camera Z-Axis vector times ten.
Where do I apply that script?
BTW I added my movement script to the question, that may help you answer...
impulseDirection should be in your rigidbody.AddTorque(). Right now you are adding torque in the world axes rather than on the local axes. You could use AddRelativeTorque ins$$anonymous$$d with the code you already have (I assumed you were using forces). Try both ways...
Ok problem solved...
Basically I ended up using a modified version of the example script under TransformDirection in the scripting reference. Without you I may not have found out about TransformDirection, so Thank You!!!
Answer by Techsuport · Aug 19, 2011 at 03:13 AM
also you can go..
transform.forward*10
=
transform.TransformDirection(Vector3(0,0,10)
both give transform relative vectors