Hey, I am making a 2D game for fun and to show to my friends. I am watching youtube videos to help me learn code. My game was working until I tried to make it so when my player jumped backwards the character would show the jumping animation backwards. Somewhere I messed up and need help. As soon as I start my game, my player just flies up. If I uncheck the playerController script then nothing happens, so I know it;s in the script. If someone could tell me what;s wrong it would mean a lot. Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private float moveVelocity;
public float jumpHeight;
public Transform groundCheck;
public float GroundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent ();
}
void FixedUpdate (){
grounded = Physics2D.OverlapCircle (groundCheck.position, GroundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
anim.SetBool (“Grounded”, grounded);
//jump
if(Input.GetKeyDown(KeyCode.Space) && grounded)// <---- can only jump on ground
{
//rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, jumpHeight);
jump ();
}
//dbl jump
{
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, jumpHeight);
jump ();
doubleJumped = true;
}
moveVelocity = 0f;
//move right
if(Input.GetKey(KeyCode.D))
{
rigidbody2D.velocity = new Vector2 (moveSpeed, rigidbody2D.velocity.y);
moveVelocity = moveSpeed;
}
rigidbody2D.velocity = new Vector2 (moveVelocity, rigidbody2D.velocity.y);
//move left
if(Input.GetKey(KeyCode.A))
{
rigidbody2D.velocity = new Vector2 (-moveSpeed, rigidbody2D.velocity.y);
moveVelocity = -moveSpeed;
}
anim.SetFloat(“Speed”, Mathf.Abs(rigidbody2D.velocity.x));
if(rigidbody2D.velocity.x > 0)
transform.localScale = new Vector3( 1f, 1f, 1f);
else if (rigidbody2D.velocity.x > 0)
transform.localScale = new Vector3( -1f, 1f, 1f);
}
public void jump ()
{
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, jumpHeight);
}
}
Thank You in advance:)