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 Borzi · Sep 27, 2013 at 05:50 PM · rigidbodyvelocitysmoothdampref

rigidbody.velocity problem

Hey I was wondering why this line of coding is not working. I am getting the following error: "Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable". Anybody know what I can do?

Update: When the rigidbody.velocity line variable is above the horizontalMovement, than I get very jerky results but the stopping works. If I do it vice versa, movement works fine but stopping doesn't.

 //Checks how fast player is moving
     horizontalMovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
     rigidbody.velocity = new Vector3(horizontalMovement.x, rigidbody.velocity.y, horizontalMovement.y);
         
     //If movement exceeds Max Walk Speed
     if(horizontalMovement.magnitude > maxWalkSpeed)
         {
         //Normalize it
         horizontalMovement = horizontalMovement.normalized;
         //Reset it to Max Walk Speed
         horizontalMovement *= maxWalkSpeed;
         }
     
     if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded == true)
         {
             Vector3 velocity = rigidbody.velocity;
             velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeaccelerationVolx, walkDeacceleration);
             velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeaccelerationVolz, walkDeacceleration);
             rigidbody.velocity = velocity;
         }
         
Comment
Add comment
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

1 Reply

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

Answer by robertbu · Sep 27, 2013 at 06:15 PM

In C# you have to do:

 Vector3 velocity = rigidbody.velocity;
 velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeaccelerationVolx, walkDeacceleration);
 velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeaccelerationVolz, walkDeacceleration);
 rigidbody.velocity = velocity;

Note typing the error message along the word "Unity3d" into Google will get you lots of hits/solutions for most Unity errors.

Comment
Add comment · Show 2 · 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 Borzi · Sep 28, 2013 at 11:18 AM 0
Share

I googled it but didn't get any useful results, hence I had to ask here. Thanks for your reply. I updated the script but when the player doesn't press the buttons he doesn't slow down. Any idea why? I think this is how you meant it to be used, right?

 if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded == true)
         {
             Vector3 velocity = rigidbody.velocity;
             velocity.x = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.x, 0, ref walkDeaccelerationVolx, walkDeacceleration);
             velocity.z = $$anonymous$$athf.SmoothDamp(rigidbody.velocity.z, 0, ref walkDeaccelerationVolz, walkDeacceleration);
             rigidbody.velocity = velocity;
         }
avatar image robertbu · Sep 28, 2013 at 01:29 PM 0
Share

I don't see anything in this code that would account for what you describe. Is there any other code that accesses the keys?

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

15 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

Related Questions

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

rigidbody.velocity.normalized application 1 Answer

Why is velocity checking intensive? 0 Answers

Is there a way to set velocity while setting the position? 1 Answer

Velocity of multiple rigidbody obiects in array 4 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