- Home /
Can relative velocity be calculated from relative direction vector
Let's say i got direction vectors for an object relative to the camera looking at it as explained here
http://answers.unity3d.com/questions/437111/calculate-vector-exactly-between-2-vectors.html
Is it possible for me to derive the relative velocity in those directions using the relative direction vectors and some other information like magnitude or something?
Or do i have to find the relative velocity vectors separately the same way id find the directions themselves.
Well what i mean is how to find the velocity vector of a rigidbody from the perspective of the camera looking at it. Rigidbody.velocity returns global coordinate values, but i need to adjust them to camera coordinate values. That's what i meant.
I'm sure i can do something like this: xSpeed = camera.transform.TransformDirection(Vector3(Rigidbody.velocity.x,0,0));
but i was wondering if there was a way to use the relative camera direction vectors i found earlier ins$$anonymous$$d of doing it like this.
hmm is there a particular reason to use the inverse and point? for the directions i just used transformdirection, i didnt do the inverse. For example, to find the direction "right" relative to the camera i simply did directionRight = camera.transform.TransformDirection(Vector3.right);
Wouldn't it be the same exact approach with velocity like this: xSpeed = camera.transform.TransformDirection(Vector3(Rigidbody.velocity.x,0,0));
Or am i misunderstanding something here at a basic level lol?
TransformDirection turns a Vector3 in camera space to world space. Like if your camera was rotated so that it was looking down the positive X world axis, transform.TransformDirection(Vector3.forward)
would return (1,0,0) since the camera's forward is looking down the world's X axis.
InverseTransformDirection does the opposite and will turn a Vector3 from world space into camera space. So in the example above with camera looking down positive X world axis, transform.InverseTransformDirection(Vector3.foward)
would return (-1,0,0) since the world's forward points to the camera's left.
From what you've said, it sounded like you wanted to get directions into your camera's local coordinate system, so you'd use the InverseTransformDirection. InverseTransformPoint would work too I think but is affected by scale.
actually judging by your excellent explanation, TransformDirection really is what i wanted, i think. For the directions for example, i wanted a world vector from the perspective of the camera and thats what TransformDirection gave me. I later use the direction vectors to AddForce to an object so i was able to successfully implement the ability to rotate the camera around an object and have the object still move "up" or "right", etc based on keypresses. Before i implemented this, i could only use one static camera view and i used AddForce from a global coordinate perspective.
It's just now i need to also know the velocity of an object going to the camera's "right","up", etc... and it seems to me like finding the world velocity from the camera's perspective is the right way to do this. Or am i getting a bit confused since now im utilizing the velocity of an object as the argument for the transformdirection function of the camera...
this is starting to get to me...
Are you wanting to get the components of the movement of the rigidbody in terms of the up and right of the camera? If you are then you want to project the movement vector onto those two vectors. You can do that with Vector3.Project
Get the world vector of the movement and project it onto all three of Camera.main.transform.forward, right and up - you can get the amount of speed in each of those directions or just use the correctly scaled vectors that are returned.
Answer by whydoidoit · Apr 16, 2013 at 06:20 PM
Did you see my comment? Project the rigidbodies velocity vector onto the .right and .up of the camera and take the magnitude...
Answer by whebert · Apr 16, 2013 at 11:26 PM
OK, that makes a little more sense now. And, if I understand you correctly, you want the forward velocity relative to the camera's "forward horizontal direction", as if the camera wasn't tilted down looking at the object?
I believe the following might do something like you need, using what @whydoidoit suggested with Vector3.Project.
Vector3 cameraForwardHorizontalVector = Vector3.Cross(transform.right, Vector3.up).normalized;
Vector3 velocityCameraForward = Vector3.Project(velocity, cameraForwardHorizontalVector);
Vector3 velocityCameraBackward = velocityCameraForward * -1;
Vector3 velocityCameraRight = Vector3.Project(velocity, transform.right);
Vector3 velocityCameraLeft = velocityCameraRight * -1;
The above code would be attached to the camera, not sure how you have things setup.
if i hold the right key and rotate the camera around, i start getting forward velocity. Same thing happens with whydoidoit's implementation and my own implementation... im trying to wrap my $$anonymous$$d around why thats happening and how to work with it.
Thing is in my game i have something like this:
if key press up then if forward vector magnitude < threshhold ADDFORCE UP
BUT i also slow down the object on right and left to 0 and i do it like this
if x vector.x velocity > .1 then ADDFORCE left if x vector.x velocity < -.1 then ADDFORCE right
then drag takes care of smoothing and stuff
But when i rotate camera and the x and z velocity components start jumping around then my if statements get screwed up. I was thinking of calculating magnitude for the slowing down checks too but then its always positive i cant tell if the object is moving left or right. This is what im trying to solve now.
ohh my code is attached to the rigidbody because thats where i keep my movement code. so transform.right is related to camera? ill try to change and see
yup no matter how i do it i get same results. I think the best way to go maybe, is to do var cameraSpaceRight = Vector3.right * relativeRight.magnitude;
like whydoidoit said, because like that at least all the information is in one component. Generally im gonna try to make this work by using magnitudes and somehow figure out a way to tell in what direction the object is traveling to know which way to apply the stopping force like i mentioned above...
I feel like theres some elegant solution to this im not seeing lol... YET Ima take a break from this for today and come back tomorrow cause ive spent the whole day trying to get this to work. I hope i got my current problem across.
That its not about finding the velocity anymore but ins$$anonymous$$d to do what i described above with the information. Its the camera rotation that screws things up by creating forward velocity even when im only moving right, or vice versa.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100434im_/https://answers.unity.com/themes/thub/images/avi.jpg)