x


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); 
}
more ▼

asked Dec 19 '10 at 05:30 PM

DonGato gravatar image

DonGato
20 6 7 12

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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.

more ▼

answered Dec 19 '10 at 05:58 PM

The_r0nin gravatar image

The_r0nin
1.4k 9 14 30

Where do I apply that script?

BTW I added my movement script to the question, that may help you answer...

Dec 20 '10 at 06:29 AM DonGato

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 instead with the code you already have (I assumed you were using forces). Try both ways...

Dec 20 '10 at 01:14 PM The_r0nin

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!!!

Dec 20 '10 at 06:04 PM DonGato
(comments are locked)
10|3000 characters needed characters left

also you can go..

       transform.forward*10
                =
transform.TransformDirection(Vector3(0,0,10)

both give transform relative vectors

more ▼

answered Aug 19 '11 at 03:13 AM

Techsuport gravatar image

Techsuport
81 6 6 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x2999
x1790
x1367
x804

asked: Dec 19 '10 at 05:30 PM

Seen: 2427 times

Last Updated: Aug 19 '11 at 03:13 AM