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.
But the Problem is that if now I do addtorque to my cube the result is not good anymore
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 ?
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.
I know that my sample code is totaly not optimized at all it was just to show you what I want to obtain
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.
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
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
}