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 /
avatar image
0
Question by 890evil · Aug 14, 2011 at 12:54 PM · addforce

Rolling Ball Code Isn't Working

I am new at Unity and I have been trying to make a code for a game similar to super monkey ball or marble blast gold. The problem is that the code I am using isn't working. Can someone please help. Here is my code.

 function Update () {
 if (Input.GetKey ("up")) 
 rigidbody.AddForce (Vector3.foward * 10);
 
 if (Input.GetKey ("down")) 
 rigidbody.AddForce (Vector3.backward * 10);
 
 if (Input.GetKey ("left"))
 rigidbody.AddForce (Vector3.left * 10);
 
 if (Input.GetKey ("right"));
 rigidbody.AddForce (Vector3.right * 10);
 }
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

6 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by datorum · Aug 14, 2011 at 01:44 PM

There are several possibilities here. One could be that the applied force is too small another one could be that you apply the force to the wrong rigidBody or you don't even receive the keyboard input. Try the following:

 function Update () {
 if (Input.GetKey ("up"))
 {
    rigidbody.AddForce (Vector3.foward * 10);
    Debug.Log("up pressed");
 }
 ...
 }


if you don't get a message in the console, you know that the keyboard never "arrives" correctly. If you get a message just increase 10 to 1000 or something way bigger. Trial and Error: just eliminate different possible error sources one after another.

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
avatar image
1

Answer by Waz · Aug 14, 2011 at 01:50 PM

To roll a ball, I recommend AddTorque (I.e. spin it) and let friction do the work. Otherwise you get unnatural effects, like in-air acceleration. For playability, a combination may be needed.

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 890evil · Aug 14, 2011 at 02:47 PM 0
Share

It says NullReferenceException Object Reference not set to an instance of an object.

avatar image Waz · Aug 14, 2011 at 02:51 PM 0
Share

Don't you think you should edit your Question with that vital information? Way more intelligent and informative than "isn't working".

avatar image
0

Answer by djfluffwug · Oct 07, 2011 at 04:05 PM

The problem is you forgot to put in the {}'s.

Wrong Way:

if (Input.GetKey ("down")) rigidbody.AddForce (Vector3.backward * 10);

...

Right Way:

if (Input.GetKey ("down")) { rigidbody.AddForce (Vector3.backward * 10); }

Comment
Add comment · Show 3 · 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 syclamoth · Oct 07, 2011 at 04:07 PM 0
Share

No, that format works. It's not the prettiest, or the safest, but it is legal code.

avatar image djfluffwug · Oct 07, 2011 at 04:22 PM 0
Share

Oh does it? Wow, I guess I learnt something new. I tried that a few months ago and it didn't work. ($$anonymous$$ust have stuffed up somewhere else). Thanks for the heads up.

avatar image syclamoth · Oct 07, 2011 at 04:35 PM 0
Share

it works, but it's still not good to use.

avatar image
0

Answer by djfluffwug · Oct 08, 2011 at 05:56 AM

Oh, I see one of the problems.

You have spelt Forward incorrectly. It is Forward, not foward.

Also, there is no Backward in unity. You have to instead use forward but the negative of it.

For example:

 if (Input.GetKey ("down"))
 rigidbody.AddForce (Vector3.forward * -10;
 
 
 //This will give the ball a force of -10 in the forward direction meaning the ball will roll backwards.
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
avatar image
0

Answer by thegdeveloper · Feb 28, 2013 at 03:00 AM

Never use the addForce and addTorque, basically anything that has to do with the physics on update. You must applythem on FixedUpdate

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
  • 1
  • 2
  • ›

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rigidbody.AddForce not working when Animator is attached? 1 Answer

AddForce from Two Sources 1 Answer

Why moving characters laterally with addForce fails? 1 Answer

Smooth out character movement? 4 Answers

RigidBody2D Performance with AddForce on WakeUp 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