- Home /
Finding Velocity at point but locally....
Hi, Im not sure why im finding this so difficult but I am for some reason. I have a moving body which can go in all directions and I am trying to find the velocity in the X direction of a POINT on that object.
I can find the X Velocity of the rigid body by simply doing the following:
float xVel = transform.InverseTransformDirection(targetObj.GetComponent<Rigidbody>().velocity).x;
The above works fine, but this doesnt do at a specific point...
So I have tried so far:
leftVel = this.GetComponent<Rigidbody> ().GetRelativePointVelocity (leftPos.transform.localPosition).x;
But leftvel still changes depending on the direction of the rigidBody...
I have tried using just the GetPointVelocity and then inverseTransformDirection but I get the same problem?
I have tried the above but also transforming the local leftPos but same problem...
What am i doing wrong?
Thanks
RigidBody.Velocity is in WorldSpace, so when you do pass it through InverseTransformDirection, you are converting it to local/model Space coordinates. Is that your intention? You want the model-space velocity of a point?
Also note that GetRelativePointVelocity takes as a parameter, a world-space coordinate, but you are passing it a localPosition (modelspace) coordinate.
So: to get the local velocity of the world point:
leftVel = transform.InverseTransformDirection((this.GetComponent ().GetRelativePointVelocity (leftPos.transform.position)).x;
Edir: oops! I was looking at GetPintVelocity, Not getPointVelocityRelative in the docs! ignore all that pls.. ok, after review- I think issue may still be the "space" of the point you are providing. note that this objects local modelSpace, leftPos.transform.localPosition
, is NOT the same model space as this.transform
. In order to get it to the correct space, you would need to apply the inverse of this.Transform to the world space coordinate of the leftPos transform. e.g.
localPositionToCheck = transform.InverseTransformPoint(leftPos.transform.position);
In this case, I'm assu$$anonymous$$g that the doc's "RelativePoint" is in localSpace: I have NOT tested this. they could mean relative in world-space. in which case we would want to use:
positionToCheck = transform.position-leftPos.transform.position;
Answer by oswin_c · Dec 20, 2016 at 03:50 PM
I am assuming that you are trying to get the point velocity relative to transform.left of the rigidbody.
I look at your code and I cannot see what is going wrong . If local to world space transforms, coordinate systems, the meaning of .x in local space, confuse you, and have unexpected results, you are by no means alone -- I have the same problem and I'm no girl next door.
This is why I generally discourage using .x, .y, .z components if at all possible -- there's no reason to make yourself dependent on a coordinate system when we have vectors and quaternions. There's a simple and slick way of doing this that will give you the point velocity no matter what.
My suggestion (not tested) is to start by trying this:
leftVel = Vector3.Dot(
this.GetComponent<RigidBody>().GetRelativePointVelocity(leftPos.transform.localPosition),
this.transform.left
);
If the code is doing what you think it is doing, this should give you exactly the same answer as getting the .x component. In that case, I suggest taking a close look at your object's behavior -- maybe that makes sense. However, I have a hunch it's not doing what you think it's doing, and I've run into problems with using components in unity before, which is why I stopped.
Your answer
Follow this Question
Related Questions
Throwing knife doesn't throw correctly 0 Answers
Angular Friction 0 Answers
Convert from m/s to m/fixedFrame 1 Answer
Naturally make velocity 0 2 Answers
Problems with rotating an object using angular velocity. 2 Answers