Getting xy vector component of velocity

Hi,

I’m developing a little game and I need to get a vector that is based on the x and y component of the velocity.(the same vector as velocity but with z=0)

But I’m having an issue :

Here is a sample code

function Update () {
var cube = GameObject.Find("Cube");
var v :Vector2;

v =cube.rigidbody.velocity;
Debug.Log(cube.rigidbody.velocity);
cube.rigidbody.AddForce(cube.transform.TransformDirection(Vector3.right)*0.1,ForceMode.Force);
cube.rigidbody.AddForce(cube.transform.TransformDirection(Vector3.forward)*0.1,ForceMode.Force);

Debug.DrawRay (cube.transform.position,cube.rigidbody.velocity,Color.red);
Debug.DrawRay (cube.transform.position,v,Color.green);

}

with this code I get what i want the red vector is the velocity and the green vector is the velocity without the z component.

here is the top View of the scene : TopView

But the Problem is that if now I do addtorque to my cube the result is not good anymore :frowning:

Here is the result after letting the cube turn a little bit : Twitpic
The Green vector stay in the “world” xy plane but it should stay in the xy plane of the Cube. (the result intended would look like the blue vector but i cant get it to work)

Maybe you guys have an idea of what i’m doing wrong ?

Thanks for your help !

Batiste

1247201--53625--$771004305.jpg
1247201--53626--$771005807.jpg
1247201--53627--$topView2.jpg

Edited with link to the images. embeded ones disapeard =/

Any ideas on how to get it work guys ?

I have no idea what you are trying to do. First, you call to find the object cube in every frame. That is a total waste of processing power. Call it once and cache it at start. Second, you should just define the var v, as a global, to the script. It will just clean up the code a bit.

Then you are receiving the rigidbody velocity. But you are setting it also, so, why ask for it? Also, Update, should not be used for physics. FixedUpdate is for that.

Lastly, you say everything works except for when you add torque. Well, where do you do that? It’s not in your code.

Thanks for your response and advice.

I know that my sample code is totaly not optimized at all it was just to show you what I want to obtain :wink:
I’ll try to explain myself better:
In fact I have an object that is moving in 3d space.

And I want to get the velocity vector of this object. but ignoring z component of this velocity in order to do some calculation.
I need a vector representing speed on the xy plane.

Imagine that cube.rigidbody.velocity return me something like Vector3(0,1,1) the result i want is Vector3(0,0,1)
and then i want to draw it using drawray.

This is done by doing

var v :Vector2;
v = cube.rigidbody.velocity;

and

Debug.DrawRay (cube.transform.position,cube.rigidbody.velocity,Color.red);
Debug.DrawRay (cube.transform.position,v,Color.red);

with this would be able to see 2 vector like this :

Here it is good (cube with no rotation):

But the problem is that after rotation :

cube.rigidbody.AddTorque((Vector3.up *1));

I obtain this :

while i would like to obtain this :

i hope that this time my explanation is better ^^

Thanks for help !

Anyone Got an idea on how to obtain desired result ?

Velocity is local to the object but you’re drawing it in world space so you need to transform it from local to world using transform.TransformDirection

Hey !

I had already tryed to use transform.TransformDirection but i was using it the wrong way.

here is the code that solve the problem :

var cube:GameObject;
var v :Vector2;

function Start () {
cube = GameObject.Find("Cube");
}

function Update () {

v = cube.transform.InverseTransformDirection(cube.rigidbody.velocity);   //here is the importante line

Debug.Log(cube.rigidbody.velocity);

cube.rigidbody.AddForce(cube.transform.TransformDirection(Vector3.right)*0.1,ForceMode.Force);
cube.rigidbody.AddForce(cube.transform.TransformDirection(Vector3.forward)*0.1,ForceMode.Force);
cube.rigidbody.AddTorque((Vector3.up *0.00001));

Debug.DrawRay (cube.transform.position,cube.rigidbody.velocity,Color.red);
Debug.DrawRay (cube.transform.position,cube.transform.TransformDirection(v),Color.green); //and here too
}

You helped me to think the right way thanks !