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 ProjectCryken · Jul 20, 2013 at 06:04 PM · rigidbodyvelocitylocal

Set The Local Velocity Of A Rigidbody

I am currently using a smoothdamp function to de-accelarate my player's velocity. However, because velocity.x and .z aren't local, it doesn't work properly.

 if (grounded)
     {
         if (!Input.GetButton("Horizontal"))
             rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVelx, walkDeacceleration);
         if (!Input.GetButton("Vertical"))
             rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVelz, walkDeacceleration);
         
     }

So what do I do to the above code to make each statement act on the rigidbody's local velocity?

Comment
Add comment · Show 4
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 robertbu · Jul 20, 2013 at 06:11 PM 0
Share

In think you can just use transform.InverseTransformDirection() on the velocity, do the damping, and transform the vector back to world space with transform.TransformDirection().

avatar image ProjectCryken · Jul 20, 2013 at 06:51 PM 0
Share

Sorry, I don't understand. Could you repost my above code with that adjustment? As I am unsure of how I would apply a vector to the .x and .z of a different vector.

avatar image sparkzbarca · Jul 20, 2013 at 06:54 PM 0
Share

do you have a good reason for directly affecting velocity ins$$anonymous$$d of doing it the proper way? I.$$anonymous$$ adding force in the inverse direction of velocity. You could simply do add force (-velocity) and stop when velocity is approx vector3.zero or at that point manually set velocity to zero. Generally speaking if your going to use a rigidbody use physics to change stuff, otherwise dont use a rigidbody.

avatar image ProjectCryken · Jul 20, 2013 at 07:00 PM 0
Share

Yes, as the decrease in velocity must be deter$$anonymous$$ed by the current velocity and it must be done smoothly, which I am unsure of how to do with add force as I don't know how to actually get the local velocity. $$anonymous$$y way in theory works great, if I can set the local velocity.

2 Replies

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

Answer by robertbu · Jul 20, 2013 at 07:03 PM

Here is what I was talking about (untested):

 var v3Velocity = transform.InverseTransformDirection(rigidbody.velocity);
 if (!Input.GetButton("Horizontal"))
     v3Velocity.x = Mathf.SmoothDamp(v3Velocity, 0, walkDeaccelerationVelx, walkDeacceleration);
 if (!Input.GetButton("Vertical"))
     v3Velocity.z = Mathf.SmoothDamp(v3Velocity.z, 0, walkDeaccelerationVelz, walkDeacceleration);
 rigidbody.velocity = transform.TransformDirection(v3Velocity);
Comment
Add comment · Show 1 · 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 ProjectCryken · Jul 20, 2013 at 07:10 PM 0
Share

Thanks mate ;)

avatar image
0

Answer by sparkzbarca · Jul 20, 2013 at 07:10 PM

you can get the local velocity via transform.inversetransformdirection(velocity)

however -velocity is the going exactly the opposite direction in world space so you can stay in world space.

 //braking power should be a value between 0 and 1 probably
 //a breaking force of 0 will not stop you a breaking force of 1 should stop
 // you completely nearly instantly
 
 float BrakingPower; 
 bool Brake;
 
 void FixedUpdate()
 {
 if(Brake)
 rigidbody.addforce(-rigidbody.velocity * BrakingPower * time.deltatime);
 }
 }

that should do it.

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

16 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How do I obtain the local velocity of a Rigid body on one axis? 3 Answers

Local Velocity for Rigidbody? 1 Answer

Velocity relative to Local Axis 1 Answer

How do you directly add to velocity in a relative manner? 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