Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by luniac · Apr 16, 2013 at 03:50 PM · cameravelocityvectorrelative

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.

Comment
Add comment · Show 18
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image luniac · Apr 16, 2013 at 04:48 PM 0
Share

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.

avatar image luniac · Apr 16, 2013 at 05:09 PM 0
Share

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?

avatar image whebert · Apr 16, 2013 at 05:28 PM 0
Share

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.

avatar image luniac · Apr 16, 2013 at 05:46 PM 0
Share

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

avatar image whydoidoit · Apr 16, 2013 at 05:55 PM 0
Share

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.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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;
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whebert · Apr 16, 2013 at 11:35 PM 0
Share

The above code would be attached to the camera, not sure how you have things setup.

avatar image luniac · Apr 16, 2013 at 11:45 PM 0
Share

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.

avatar image luniac · Apr 16, 2013 at 11:53 PM 0
Share

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

avatar image luniac · Apr 16, 2013 at 11:57 PM 0
Share

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

avatar image luniac · Apr 16, 2013 at 11:58 PM 0
Share

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.

Show more comments
avatar image
0

Answer by DaveA · Apr 16, 2013 at 07:12 PM

What whydoidoit said

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Relative player controls velocity to camera rotation. 2 Answers

Move JoyStick Together With Camera 1 Answer

Rotate a Vector Relative to Another Vector 1 Answer

Calculate vector exactly between 2 vectors 4 Answers

Create a cube relative to camera & mouse position. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges