Why can my 2D character only look around but not move?,Why is my character only able to look around but not move?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController2D : MonoBehaviour {
SpriteRenderer spriteRenderer;
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb2d;
private bool isGrounded;
private int extraJumps;
public int extraJumpsValue;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGrounded;
// Start is called before the first frame update
void Start()
{
extraJumps = extraJumpsValue;
rb2d = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGrounded);
moveInput = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveInput * speed, rb2d.velocity.y);
if (Input.GetKey("d"))
{
spriteRenderer.flipX = false;
}
if (Input.GetKey("a"))
{
spriteRenderer.flipX = true;
}
}
void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKey("space") && extraJumps > 0)
{
rb2d.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if (Input.GetKey("space") && extraJumps == 0 && isGrounded == true)
{
rb2d.velocity = Vector2.up * jumpForce;
}
}
}
Comment