Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Nov 11, 2016 at 05:43 AM by Brylos for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by Brylos · Nov 10, 2016 at 07:16 PM · rigidbodyvelocity

How to get rigidbody velocity?

How can I get a rigidbody velocity that I can make the value a float? Any help would be appreciated :)

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

3 Replies

  • Sort: 
avatar image
9
Best Answer

Answer by ElijahShadbolt · Nov 11, 2016 at 01:32 AM

 float speed = rigidbody.velocity.magnitude;

For example

 float maxSpeed = 1.0f; // units/sec

 void FixedUpdate() {
     Rigidbody rb = GetComponent<Rigidbody>();
     Vector3 vel = rb.velocity;
     if (vel.magnitude > maxSpeed) {
         rb.velocity = vel.normalized * maxSpeed;
     }
 }
Comment
Add comment · Show 4 · 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 Brylos · Nov 11, 2016 at 05:42 AM 1
Share

Awesome dude! thanks a ton :D

avatar image ElijahShadbolt Brylos · Nov 11, 2016 at 06:59 AM 0
Share

Happy to help :D

avatar image wiiiteekk · Mar 15, 2017 at 04:24 PM 1
Share

Remember that in some cases you might want to use sqr$$anonymous$$agnitude ins$$anonymous$$d of magnitude which is much faster to compute

avatar image ElijahShadbolt wiiiteekk · Mar 16, 2017 at 04:07 AM 0
Share

Just replace if statement with

 if (vel.sqr$$anonymous$$agnitude > maxSpeed*maxSpeed) {
avatar image
0

Answer by hexagonius · Nov 10, 2016 at 07:42 PM

the velocity is a vector3. call magnitude on it and it returns its length as float

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 Brylos · Nov 10, 2016 at 07:48 PM 0
Share

I'm sorry. I'm kinda newbie. How exactly would I write that? I have this: PlayerBody.velocity.magnitude = CurrentSpeed;, but It gives me an error "property or indexer 'Vector3.magnitude cannot be assigned to -- it is read only"

avatar image
0

Answer by ThePersister · Nov 10, 2016 at 10:04 PM

Rigidbody.velocity is a Vector3 because it's not only an Amount of speed, it's also a Direction. If you wanted an object move forward for example, you would apply the following:

 using UnityEngine;
 using System.Collections;
 
 public class MovingObject : MonoBehaviour
 {
     public float m_speed = 20f;
     private Rigidbody m_rigidbody;
 
     void Start()
     {
         m_rigidbody = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         // Makes this object move forward at X speed.
         m_rigidbody.velocity = m_speed * transform.forward;
     }
 }

To apply force in a different direction you would just change "transform.forward" to something else like, choose one to your liking:

         m_rigidbody.velocity = m_speed * Vector3.up;  // Move Up
         m_rigidbody.velocity = m_speed * Vector3.left;  // Move left
         m_rigidbody.velocity = m_speed * Vector3.right;  // Move right
         m_rigidbody.velocity = m_speed * Vector3.back;  // Move back

I hope that helps, I'm always around for further questions, best of luck!! @Brylos

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 enes23 · Jul 31, 2019 at 07:05 PM 0
Share

Thnx bro ..

Follow this Question

Answers Answers and Comments

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

Clamped position, but Velocity not stopping. 0 Answers

C# keep previous velocity while jumping, previous solutions not working 0 Answers

How can I move an object in the direction another object is facing. 1 Answer

Bullet shooting not working 0 Answers

Slow falling speed on player (with RB), but all other objects are fine (checked scaling). 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