Problem with AddForce

I am making a game for Android and I am using the Mobile Standard Assets’ Side Scroller script because I am relatively new to unity and programming. The problem is that I can’t understand some parts of the script thus making it difficult for me to find the errors. If someone could help me I would be so glad.
PD: I am using JavaScript.

#pragma strict

@script RequireComponent( CharacterController )

// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var jumpTouchPad : Joystick;

var moveRight : KeyCode;
var moveLeft : KeyCode;
var jumpKey : KeyCode;

var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var jumpSpeed : float = 16;
var inAirMultiplier : float = 0.25;					// Limiter for ground speed while jumping

private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3;						// Used for continuing momentum while in air
private var canJump = true;
private var x : float;

function Start()
{
	// Cache component lookup at startup instead of doing this every frame		
	thisTransform = GetComponent( Transform );
	character = GetComponent( CharacterController );	
	x = transform.localScale.x;

	// Move the character to the correct start position in the level, if one exists
	var spawn = GameObject.Find( "PlayerSpawn" );
	if ( spawn )
		thisTransform.position = spawn.transform.position;
}

function OnEndGame()
{
	// Disable joystick when the game ends	
	moveTouchPad.Disable();	
	jumpTouchPad.Disable();	

	// Don't allow any more control changes when the game ends
	this.enabled = false;
}

function Update()
{
	var movement = Vector3.zero;

	// Apply movement from move joystick
	if ( moveTouchPad.position.x > 0 || Input.GetKey(moveRight)){
		transform.localScale.x = x;
		movement = Vector3.right * forwardSpeed * 2 /*moveTouchPad.position.x*/;
	}
	else if(Input.GetKey(moveLeft) || moveTouchPad.position.x < 0) {
		transform.localScale.x = -x;
		movement = Vector3.right * backwardSpeed * -2 /*moveTouchPad.position.x*/;
	}
	// Check for jump
	if ( character.isGrounded )
	{		
		var jump = false;
		var touchPad = jumpTouchPad;
			
		if ( !touchPad.IsFingerDown() || Input.GetKey(jumpKey))
			canJump = true;
		
	 	if ( canJump  Input.GetKey(jumpKey) /*touchPad.IsFingerDown()*/ )
	 	{
			jump = true;
			canJump = false;
	 	}	
		
		if ( jump )
		{
			// Apply the current movement to launch velocity		
			velocity = character.velocity;
			velocity.y = jumpSpeed;	
		}
	}
	else
	{			
		// Apply gravity to our velocity to diminish it over time
		velocity.y += Physics.gravity.y * Time.deltaTime;
				
		// Adjust additional movement while in-air
		movement.x *= inAirMultiplier;
//		movement.z *= inAirMultiplier;
	}
		
	movement += velocity;	
	movement += Physics.gravity;
	movement *= Time.deltaTime;
	
	// Actually move the character	
	character.Move( movement );
	
	if ( character.isGrounded )
	{
		// Remove any persistent velocity after landing	
		velocity = Vector3.zero;
	}
	
}
function OnCollisionEnter (other : Collision)
{
	if (other.gameObject.tag == "Bounce")
	{
		rigidbody.AddForce(0, 10, 0); 
	}
}

Sorry if I had any grammar errors, not a native English speaker.
As pointed by MDragon I didn’t specify the problem. When the object(player) collides with the bouncy platform(tag: “Bounce”) it doesn’t bounce (addForce).
Thank you.

I wish I could help you, but I’m not sure what you need help with. The topic says “Problem with AddForce,” but the info given only tells me you don’t understand parts of the entire script so you can’t find errors. What parts don’t you understand, what are you trying to do, and any other info you think would be necessary would be helpful.

Sorry for being so up-front about it, time to throw in a :smile: to make it less forward and demanding. :slight_smile:

Sorry. When the object(player) collides with the bouncy platform(tag: “Bounce”) it doesn’t bounce (addForce).

A Character Controller doesn’t have a Rigidbody? dafuq

I guess there’s no need to reverse engineer the entire code then (yay). The problem with AddForce is that it’s adding a FORCE, not changing the velocity directly. You can either up that 10 value big time (or mess around with it however you’d like) to see results. For jumping, I tend to like just doing the following:
rigidbody.velocity.y = new Vector3 (0,jumpSpeed,0); // Sets an absolute velocity, no matter the mass or current speed
(the above is useful for jump pads or such. Made a similar version before just to add to the current velocity)

But in your case, if you want to not have an absolute bounce speed and rather have it actually do a force (and thus take into consideration the mass and such), then use addForce. You can also do some other math to make the bounce depend on your falling speed- or even better yet, use the Physics materials for bouncing (since a built-in thing like that will save time and I’m pretty sure performance if you’re just going to copy the performance done by the physics engine itself and its materials :slight_smile: )

Oh, and just in case it’s something else, here’s a debugging tip: Throw in print(“Bounced… supposedly?”); within the if statement to see if it registers. (If that doesn’t register, then check if the collision at all registers by the throwing that print or a Debug.log inside the CollisionEnter but outside of the if).

Edit: Oh dang, Dave pointed something else out. The Character Controller is meant to be used WITHOUT forces. So, basically, either use the Character Controller or use rigidbodies and forces.

I knew I should’ve actually read the script… at all :smile:

Edit2: You can most likely still assign velocities and the like, if that velocity code was already built-in (read the Docs recently, said that the Character Controller is meant to be used without forces and the like, a useful and well-developed alternatives that still interacts with colliders basically).

do something like

movement = new Vector3(0,10,0);

or

movement.y = 10;

Thank you. I didn’t know a character controller couldn’t have rigidbody and this was the problem.

Okay so I searched in the forums and it seems that OnCollisionEnter() doesn’t work with character controllers but if you put the code in the collider(bouncy platform) and make it a rigidbody you can use OnCollisionEnter().