- Home /
2d Character won't jump
I am current going through Microsoft Virtual Academy and I have come across one of their scripts which won't let me jump. I assume it is because of the changes from Unity 4 to 5(Unity updated this script for me), but I can't figure it out.
I added the statement if(Input.GetButtonDown("Fire1")) {} as it seemed to be missing from the downloaded script but I cannot figure what else could be wrong.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//This script makes the character move when the screen is pressed and handles the jump
public class CharacterFinal : MonoBehaviour
{
public bool jump = false; // Condition for whether the player should jump.
public float jumpForce = 10.0f; // Amount of force added when the player jumps.
private bool grounded = false; // Whether or not the player is grounded.
public int movementSpeed = 10; // The vertical speed of the movement
private Animator anim; // The animator that controls the characters animations
void Awake()
{
anim = GetComponent<Animator>();
}
//This method is called when the character collides with a collider (could be a platform).
void OnCollisionEnter2D(Collision2D hit)
{
grounded = true;
}
//The update method is called many times per seconds
public void Jump()
{
if (Input.GetButtonDown("Fire1")) {
// If the jump button is pressed and the player is grounded and the character is running forward then the player should jump.
if (grounded == true) {
jump = true;
grounded = false;
//We trigger the Jump animation state
anim.SetTrigger ("Jump");
}
}
}
public void Slide()
{
if(grounded == true)
{
//slide = true;
//We trigger the Jump animation state
anim.SetTrigger("slide");
}
}
//Since we are using physics for movement, we use the FixedUpdate method
void FixedUpdate ()
{
//if died that
GetComponent<Rigidbody2D>().velocity = new Vector2(movementSpeed, GetComponent<Rigidbody2D>().velocity.y );
//else
//moving
// If jump is set to true we add a quick force impulse for the jump
if(jump == true)
{
// Add a vertical force to the player.
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce),ForceMode2D.Impulse);
// We set the variable to false again to avoid adding force constantly
jump = false;
}
}
}
Answer by Socrates · Mar 23, 2015 at 02:53 AM
In the code you posted, you have a function Jump() that checks if the button is pressed and sets up the jump if it is pressed. However, nowhere in the code you posted is that function called. You probably want to insert a call to Jump() just before checking the value of 'jump' on line 68.
Answer by gsapundz · Apr 09, 2015 at 04:30 AM
Try disabling the 'Apply Root Motion' checkbox from the Animator Component of your Character