x


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);
}
more ▼

asked Aug 14 '11 at 12:54 PM

890evil gravatar image

890evil
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Aug 14 '11 at 01:44 PM

datorum gravatar image

datorum
31 1 3 5

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 14 '11 at 01:50 PM

Waz gravatar image

Waz
6.5k 22 33 71

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

Aug 14 '11 at 02:47 PM 890evil

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

Aug 14 '11 at 02:51 PM Waz
(comments are locked)
10|3000 characters needed characters left

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.
more ▼

answered Oct 08 '11 at 05:56 AM

djfluffwug gravatar image

djfluffwug
16 8 8 10

(comments are locked)
10|3000 characters needed characters left

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); }

more ▼

answered Oct 07 '11 at 04:05 PM

djfluffwug gravatar image

djfluffwug
16 8 8 10

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

Oct 07 '11 at 04:07 PM syclamoth

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

Oct 07 '11 at 04:22 PM djfluffwug

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

Oct 07 '11 at 04:35 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 28 at 03:00 AM

thegdeveloper gravatar image

thegdeveloper
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x245

asked: Aug 14 '11 at 12:54 PM

Seen: 1090 times

Last Updated: Feb 28 at 03:00 AM