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.