I am trying to get my player to move based on user input but every time
public class Movement : MonoBehaviour
{
//animation
public Animator animator;
//rigidbody
private Rigidbody2D rb;
//runspeed
public float Speed;
//jumpforce
public float JumpForce;
//if it is jumping
private bool jumping;
private bool isGrounded;
//collsion stuff we dont understand yet
public Transform FeetPos;
public float CheckRadius;
public LayerMask WhatIsGround;
//the components of horizontal velocity
private float horivelocity;
//stores user inputs
private float vert;
private float hori;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
//how it jumpeth
void jump()
{
//does collision stuff somehow
isGrounded = Physics2D.OverlapCircle(FeetPos.position, CheckRadius, WhatIsGround);
//stores the value for user input
vert = Input.GetAxis("Vertical");
//checks if its on ground then when up arrow is PRESSED the velocity component is assigned to jumpforce
if(isGrounded == true && Input.GetKeyDown(KeyCode.UpArrow))
{
jumping = true;
rb.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
Debug.Log("Jump is pressed");
}
//makes the object go down once the key is released
if(jumping == true && vert <= 0 && isGrounded == false)
{
jumping = false;
rb.velocity = new Vector2(horivelocity, -1);
}
}
//how it runneth
void run()
{
hori = Input.GetAxis("Horizontal");
//stores the imput in horizontal component of velocity
horivelocity = Speed * hori;
if (hori > 0 || hori < 0)
{
rb.velocity = new Vector2(horivelocity, rb.velocity.y);
}
}
// Update is called once per frame
void Update()
{
jump();
run();
animator.SetFloat("Running", horivelocity);
}
}
I test my script it doesn’t work. Any insights?