• 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 /
  • Help Room /
avatar image
0
Question by matting · Aug 07, 2016 at 01:54 PM · rigidbodyinputupdatevelocitylateupdate

Storing instantaneous velocity

Hi, first post ever here. Thanks for the help in advance. My level of Unity skill is monkey.

I am hoping to understand this more so then getting a solution from the community. Simplified, when I do something like this...

 void Update() {
   lastVelocity = Rigidbody.velocity.z;
   if (Input.GetKeyDown(KeyCode.DownArrow)) {
     if (lastVelocity == 0) { // sphere is at rest, not + or -
       // code to do something, i.e. rotate the camera facing -z.
     }
   }
 }

Background: in another script in FixedUpdate when I GetAxis the down arrow moves the sphere -z.

The action on line 5 above never executes. If I print out the velocity when I press the GetKeyDown key, it is always negative something. That is to say, if I press play and the sphere is at rest the velocity is zero. I grab the lastVelocity (should be zero), and then I press DownArrow even if I code it to grab the lastVelocity only once it still shows negative something.

Because of this, when the sphere is at rest, I can't code to have the camera rotate if the sphere is at rest and DownArrow is pressed.

Going Forward: I will want to expand the action to if (lastVelocity >= 0 && camera.Rotation.z != -180) to move the camera only when sphere is at rest or moving +z and camera isn't already rotated.

Maybe I am just not understanding the flow and execution order?

Thanks.

Comment
Add comment · Show 3
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 matting · Aug 08, 2016 at 07:56 PM 0
Share

:*(.......

avatar image Dinosaurs · Aug 08, 2016 at 08:58 PM 0
Share

I don't think you should be setting the velocity in Update(); have you tried to store lastVelocity in LateUpdate and leave the rest of this code as-is?

This code is also generally a little confusing...a rigidbody's velocity is a vector3, how come you're comparing to an int? And what is 'Rigidbody'? Is that a variable? If so, it should really be renamed, as that's the same name as the Rigidbody class, which shouldn't even compile. It reads to me as though you're accessing a static variable on Rigidbody, which makes no sense.

avatar image matting · Aug 08, 2016 at 09:25 PM 0
Share

@Dinosaurs Rigidbody represents (for simplicity sake) rb = player.GetComponent<Rigidbody>(); I have updated the question to show that storing velocity on z-axis only. Also, I don't think zero is an integer, I mean it is but it isn't. Thank you for your comments on LateUpdate() although I am not sure your reasoning. I will look into it. Thank you!

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

79 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet Firing Every Frame 2 Answers

Cant Get a Bullet to Shoot (C#) 2 Answers

,Two Similar Rigidbodies with velocity behaving differently 1 Answer

GetKeyUp does not get called sometimes. 1 Answer

Ello~ I need help with transform.parent = null; When I play it and press the DropWeapon input button it goes in a random location and doesnt move . 0 Answers


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