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);
}
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.
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.
Don't you think you should edit your Question with that vital information? Way more intelligent and informative than "isn't working".
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.
Never use the addForce and addTorque, basically anything that has to do with the physics on update. You must applythem on FixedUpdate
Have you tried debugging the speed ? In most cases for anything times Time.deltaTime the speed itself should be a value of 200, 500, etc.. But transform.Translate moves objects without any physics, as it purely just moves the objects orientation in game world space. To get an effect when a Rigidbody is concerned, and to only apply to physics, you need objectRigidbody.AddForce(value);
I managed to fix all the little bits of problems but for some reason when I hit play, the ball insists on constantly rolling backward. What’s causing this and how can I stop it?
Don't you think you should edit your Question with that vital information? Way more intelligent and informative than "isn't working".
– WazYour welcome :)
– me2539901lreplco