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 tobobox · Jun 21, 2012 at 09:52 PM · animationcontrollervelocitylocal

non-rigidbody velocity wrong in x-axis

I'm creating an animation controller that I want to use on lots of different game objects. All of which would have different controllers (physics, navmeshagent, user input, etc). I want to use the transform.localposition in order to get forward velocity relative to the object (ie. local). What I have works great except when an object moves directly in the x-axis in either direction. At that point it shows a velocity of nearly 0 (even if moving quickly). I've gone through all the forums and tried basically all the different options but nothing is working. What am I doing wrong?

See below for my latest code attempts all of which show similar results.

option #1:

 Vector3 previousPosition;
 
 float forwardVelocity(){    
         Vector3 curMove = transform.localPosition - previousPosition;
         float curSpeed = curMove.magnitude / Time.deltaTime;
         previousPosition = transform.localPosition;
         return curSpeed;
 }



option #2:

  if(Time.deltaTime > 0){
         currentPosZ = transform.localPosition.z;
         historyPosZ = posHist;  
         locVelocity = (transform.localPosition.z - posHist)/Time.deltaTime;
         posHist = transform.localPosition.z;
         return locVelocity;
     }
     else{
         return 0.0f;
     }



option #3... uses rigidbody and still doesn't work. I would rather not use rigid body though as not all objects in my game need it.

 currentVelocity = transform.InverseTransformDirection(rigidbody.velocity).z;
Comment
Add comment · Show 7
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 whydoidoit · Jun 21, 2012 at 09:57 PM 0
Share

In option 1 you would appear to be better off using position rather than local position I think. That is the objects change in world position. It should clearly be called forwardSpeed though - that's turning it into a scalar - is that what you want?

avatar image tobobox · Jun 21, 2012 at 10:27 PM 0
Share

using position ins$$anonymous$$d of localPosition would alter all the axis right? So that the forward velocity would constantly change in relation to the direction the object is moving in the world space. I want to make sure that the velocity I receive is in relation to the object. It shouldn't matter which direction the object is moving as long as they are moving in the local z-axis there should be a positive z velocity.

avatar image whydoidoit · Jun 21, 2012 at 10:29 PM 0
Share

Well localPosition is just the position of an object within it's parent - so it would change the axis only if the object is moving relative to its parent and the parent is moving relative to the world - is that what is happening?

avatar image tobobox · Jun 21, 2012 at 11:15 PM 0
Share

no, it is not an nested object. So does this mean that I should use position but then at some point use the inverseTransformDirection function to get a vector in relation to the object as opposed to the world.

avatar image whydoidoit · Jun 21, 2012 at 11:44 PM 0
Share

$$anonymous$$aybe we are talking at cross purposes here - but isn't the difference in position of the object actually relative to the object? I must be missing what you want...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tobobox · Jun 22, 2012 at 01:06 AM

whydoidoit: See the picture below to demonstrate my point. Both world and local velocity describe the velocity of the object but the local velocity rotates it's coordinates in relation to the direction that the object is facing. So you can see in the example image that the same velocity described both ways ends up with two different values. The world velocity has a negative z value where the local velocity has a positive z value.

alt text


velocity.jpg (60.1 kB)
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to last value of Get Axis after the button is released 0 Answers

Animation Override Controllers stop working 0 Answers

Getting animations to play relative to player's direction? 4 Answers

try to make my own character control with animation like- running, walk, idle. So need help and guideline how to do or start 0 Answers

Apply force to character controller? 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