Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DoctorSauce · Dec 26, 2013 at 04:13 PM · rigidbody2dnullreferenceexceptionaddforcetime.deltatime

How to add forward velocity to a Rigidbody2D?

I can't seem to figure out how to do this. I've checked the documentation, but it doesn't really explain how to do it. This is the script I'm using right now:

 var speed : int = 10;
 
 function Update ()
 {
 rigidbody2D.AddForce(transform.forward * speed * Time.deltaTime);
 }

It seems like it should be pretty simple. With this code, I'm not getting an error, but the object isn't moving at all when it spawns. I've also tried to capitalize Rigidbody2D, 'cause I'm a little unclear on how it is supposed to be capitalized, but when I do that it gives me this error:

An instance of type 'UnityEngine.Rigidbody2D' is required to access non static member 'AddForce'.

So I'm pretty sure the one without capitalization is correct. But it still isn't moving? Could anybody help me understand why it isn't moving and how I should go about fixing it? Thanks in advance for anybody's help.

Comment
Add comment · Show 1
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 baci · Dec 26, 2013 at 04:23 PM 0
Share

Capitalizing Rigidbody2D would mean calling a static class function, but as you want the rigidbody of our GameObject to move, you want to call its instance, so the code should be fine. Do you get any runtime errors? Otherwise try with a bigger speed value, depending on the mass and drag of your rigidbody object.

2 Replies

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

Answer by azmat786n · Dec 26, 2013 at 04:25 PM

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed = 10;
     public float jumpForce = 100;
     public bool isGrounded = true;
     public float old_y;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(transform.position.y <=old_y+0.1f && !isGrounded) {
             isGrounded = true;
         }
         if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow)) {
             transform.Translate(Input.GetAxis("Horizontal")*moveSpeed, 0,0);
         }
         if(isGrounded && Input.GetKey(KeyCode.Space)) {
             isGrounded = false;
             old_y = transform.position.y;
             rigidbody2D.AddForce(Vector2.up * jumpForce);
         }
     }
 
     void OnColissionEnter(Collision col) {
         if(col.collider.tag == "ground")
             isGrounded = true;
     }
 }
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 DoctorSauce · Dec 26, 2013 at 05:12 PM 0
Share

Alrighty, this is what ended up helping me so ima mark it as the answer. I took this line:

 transform.Translate(Input.GetAxis("Horizontal")*moveSpeed, 0,0);

and turned it into this line:

 transform.Translate(moveSpeed, 0,0);

and stuck that in my update function. Worked perfectly!

avatar image azmat786n · Dec 26, 2013 at 07:54 PM 1
Share
     var speed : float = 10;
      
     function Update ()
     {
     rigidbody2D.AddForce(transform.forward * speed * Time.deltaTime);
     }
 
     //you can use your own script by some changes like 
     var speed : int = 10;
      
     function Update ()
     {
     rigidbody2D.AddForce(Vector2.right * speed * Time.deltaTime);
     }
 
     //this code is not tested but it should work...
avatar image
4

Answer by robertbu · Dec 26, 2013 at 04:29 PM

The likely problem is that you are using 'transform.forward'. Rigidbody2D works on the XY plane, and typically you would be using a sprite for your projectile. So you want to use 'transform.up' or 'transform.right' depending on how you've setup your game and your sprite. In addition, 'speed' should be a float and likely have a larger value than '10'. For an object with a mass of 1.0, start with 500.

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 DoctorSauce · Dec 26, 2013 at 05:16 PM 0
Share

Thank you very much for your help, yours was a good answer too. But alas, I can only mark one answer as correct. +1 for you, good sir.

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

20 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

Related Questions

How do i make jumps follow through with rigidbody2D.addforce? 1 Answer

Physics are different in Build and Editor 0 Answers

how to make "addForce" only affect one of several connected objects (with hinge points) 0 Answers

Acceleration and Max Speed on 2D Object 0 Answers

Add force seems to add random amount of force? 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