I have part of my script here. This part of the script make the player jump but there is a problem with it. When the user clicks on the spacebar it doesn’t do anything but when the user clicks the screen it jumps. When the player jumps the player jumps on the y axis and comes down because of gravity but the animation doesn’t change fro jump to run. I will also upload a pics of the animator components @LazyElephant the full script is now available
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Rigidbody2D))]
public class characterMovement : MonoBehaviour {
public int speed;
Animator anim;
bool grounded;
private Rigidbody2D rigidbody2D;
public Transform groundCheck;
public LayerMask ground_layers; //The layer for which the overlap has to be detected
public float jumpForce;
void Start ()
{
anim = GetComponent<Animator>();
rigidbody2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
//check if the empty object created overlaps with the 'ground_layers' within a radius of 1 units
grounded = Physics2D.OverlapCircle(groundCheck.position, 1, ground_layers);
anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
}
void Update ()
{
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
transform.Translate(speed * Time.deltaTime, 0f, 0f);
if (grounded == true && Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.Mouse0))
{
rigidbody2D.AddForce (new Vector2 (0f, jumpForce));
anim.SetBool ("Grounded", true);
Debug.Log ("Can Jump!!!");
}
else if(grounded = false)
{
anim.SetBool ("Grounded", false);
Debug.Log("Cannot Jump");
}
}
}