I can't get AddForce to work.

hello again. I need some help with a thing. the thing is that I can’t get AddForce to work. here’s my code:

if (Input.GetButton ("Addforce")) {      
		rigidbody.AddForce (Vector3.up * 10);
		print("check") ;
   }//check is just for me to see if the Get.Button works,and it sure do
   else {
   	print(" no check");
   }

I have applied a rigidbody to my object.

Try applying a larger force or lowering the mass of the rigidbody.
Make sure the rigidbody isKinematic property is false.

nope , it doesn’t work either. I’ve tried it :frowning:

It is printing “check”, I assume, right?

Also, you might have to seriously crank the amount of force you’re adding. If you increased it from 10 to 50, try 10 to 1000.

I’ve tried from 10 to 10000000 and it doesn’t work (btw, yes: it is printing check)

Are you adding the force from FixedUpdate? Is the scale of your objects correct?

I have it in a fixedupdate but what do you mean with the right scale?

not sure if this helps but. I used add force on an object using the slider… and instead of just giving it like you did.

rigidbody.AddForce (Vector3.up * 10);

I defined it in the beginning with a GameObject var. Like this.

var someObject : GameObject;

if (Input.GetButton (“Addforce”)) {
someObject.rigidbody.AddForce (Vector3.up * 10);
print(“check”) ;
}//check is just for me to see if the Get.Button works,and it sure do
else {
print(" no check");
}

Maybe it doesn’t know which object to apply the force to?

Also make sure the object you are trying to add force to has a rigidbody attched!

Here is code for a jump pad I created last night, it works perfect:

var player : Rigidbody;
var jumpforce : int;

function OnTriggerEnter () { 
		
		Debug.Log("Trigger Entered"); 
		player.AddForce (Vector3.up * jumpforce);	
	
}

I’m attaching my player which is a rigidbody to the PLAYER variable and the JUMPFORCE can be varied in the editor to try different amounts of force easily. Also my jump pad (just a simple cube prefab at the moment) is checked to be a trigger.

Hope this helps.
jamie

I assume you didn’t change the default mass value for your object and it is still 1 and scale is 1, correct? And you have verified that Vector3.up points in the direction you think it does (meaning you didn’t change your initial orientation when placing your objects and are now trying to push it through the floor). And neither in the inspector, nor in any script is the Rigidbody being set to kinematic (i.e. isKinematic = true). Finally make sure the game object containing your rigidbody is not “stuck” inside another game object. I assume it is sitting freely on a plane or terrain collider. Correct?

If everything checks out, then can you post your FixedUpdate method?

Is the object you are trying to apply a force to the first person controller? Because something in that prefab stops it from working. I think its the grounded variable or something.

For example,

This isn’t working with the first person controller, even with a force of 5000.

thx for so many replies. I just got one question. is it a prephab (of the same object) that i should add to “var player : rgidbody ;”
(btw, I’m doing a 2d platform game and I want to decrease gravity when I thouch a wall(so that you fall down slower).
And I don’t have “the main object” inside another object.)(I can walk and jump)

I haven’t been able to try all the examples that you’ve given me because my Windows have fussed very much. But please show me more ways to solve this problem :slight_smile: .

You need to create your game object and add the Rigidbody to it then as well as any script that will control it or handle collisions. Then create a prefab asset in the inspector and drag the object onto the prefab (making the object a prefab). You can then delete the original instance from the project/scene area of the inspector.

For instances of the prefab you want to create during scene design, just drag instance of the prefab and drop them into the scene area of the inspector. Alternatively you can create them in your scripts using instantiate. Once your object has been moved vertically by applying vertical force, the drag coefficient will control the rate of descent. The higher the drag value, the slower you will descend. If you want to have the object descend more slowly when you hit a wall, you can do this by having a low initial drag value and then increasing it when it impacts a wall. You do this by making the wall collider a trigger and then in the trigger script attached to the struck wall collider, increase the drag value. If you want to have it slow while in contact with the wall and then speed up when you break contact with the wall, you can do this by scripting the OnCollissionEnter and OnCollisionExit events of the wall collider.

Hi Lime_x:

Yes in the example script I posted above the “var player : Rigidbody;” gets my player prefab dragged onto it in the inspector.

Discord:

No I’m not using the first person controller in my game. I’m building a typical “marble rolling” game so my player is a marble that is a rigidbody. For this script to work your player will need to be a rigidbody for sure. I’ve never tried it but I saw on the wiki there is an FPS controller that is a rigidbody you might try that.

jamie