Hello all!
I’m making a 2D platformer game, and everything has been going well, until I added my jump sound effect.
For some reason, it affected the script so that when you try to move you can’t. And after attempting to press the left or right arrows (quickly) you can only move in that direction and can’t change it.
I don’t get what’s wrong with it. I tried starting the script again, but the exact same thing happened again. I have no idea about what happened, because I only added one line of code to the jump function, which has nothing to do with the movement. Here is the code:
PlayerController.cs:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
public Transform firePoint;
public GameObject projectile;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
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);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
//rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
Jump();
}
if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
{
//rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
Jump();
doubleJumped = true;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
if (Input.GetKey(KeyCode.RightArrow))
{
rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
anim.SetFloat("Speed", Mathf.Abs(rigidbody2D.velocity.x));
if (rigidbody2D.velocity.x > 0)
transform.localScale = new Vector3(0.3294535f, 0.3294535f, 0.3294535f);
else if (rigidbody2D.velocity.x < 0)
transform.localScale = new Vector3(-0.3294535f, 0.3294535f, 0.3294535f);
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(projectile, firePoint.position, firePoint.rotation);
}
}
}
}
void Jump() {
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
audio.Play();
}
}
I have the AudioSource on my Player object, and this code returns no errors. When it was working fine, the movement was done so that you push the appropriate key and in moves in that direction, then stops when you release said key. I explained what is wrong above the code.
Thanks in advance!