• 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
Question by RadicalM1nd · Jul 15, 2014 at 09:22 PM · movementrigidbodyvelocity

Question regarding Ridigbody2D.velocity.magnitude

Hi guys,

I'm currently trying to learn to build my own movement speed from scratch instead of relying on the Sample Assets provided by Unity. Currently, I'm trying to cap the velocity of my rigidbody so it won't get infinitely speedy. Could someone tell me what is wrong with my Script?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public float moveSpeed = 30f;
     public float maxMoveSpeed = 50f;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.LeftArrow)) {
                         rigidbody2D.AddForce(Vector3.left * moveSpeed);
         }
 
         if (Input.GetKey (KeyCode.RightArrow)) {
             rigidbody2D.AddForce(Vector3.right * moveSpeed);
         }
     if (rigidbody2D.velocity.magnitude > maxMoveSpeed) {
                         Rigidbody2D.velocity = Rigidbody2D.velocity.normalized * maxMoveSpeed;
                 }
     }
 }
 

If I understood the documentation correctly, rigidbody2d.velocity.magnitude reads the current force added to the velocity and I want the force to stay the same if the cap is reached and the button is held. Unity tells me "An object reference is required to access non-static member 'UnityEngine.Rigidbody2D.velocity'

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by rutter · Jul 15, 2014 at 09:23 PM

Big R, little r:

  • rigidbody2D is the rigidbody object attached to your GameObject

  • Rigidbody2D is the class which defines all such rigidbodies

A class defines an object, in the same way that a blueprint defines a building.

There may be functions associated with the Rigidbody2D class. For example, Vector3 provides helper functions like Distance, which have to do with vectors, but aren't related to any specific vector.

On the other hand, the data specific to a particular rigidbody (such as its velocity) can only be accessed through that object.

So this line:

 Rigidbody2D.velocity = Rigidbody2D.velocity.normalized * maxMoveSpeed;

Should read:

 rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxMoveSpeed;
Comment
recox99

People who like this

1 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 RadicalM1nd · Jul 16, 2014 at 07:33 AM 1
Share

Ah, so it was that simple :) Thanks a lot for the quick and thorough response!

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody sticks to wall 0 Answers

How do I move this rigidbody? 1 Answer

Rigidbody movement conflict? 1 Answer

How to make rigidbody movement without changing or clamping the Velocity? 3 Answers

Add force lasting 1 frame (simple script) 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