This is my player controller code and my first draft was like ““public float moveSpeed;”” like that but then i want to add runing animations to and i implemented my code "“public float runSpeed – public float runSpeed” but when i press “d” which moves my character right my player start jumping to the right and when i press “a” which moves my character left ,my character walks left
this is my animator part:
Arrows:
player idle to player run if speed greater than 8
player run to player idle if speed less than 0.1
player idle to player walk if speed greater than 4
player walk to player idle if speed less than 0.1
player run to player walk if speed less than 7.9
player walk to player run if speed greater than 8
player idle to player jump if speed grounded false
player jump to player idle if speed grounded true
player walk to player jump if speed grounded false
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// this will speed up the players movement
// public float moveSpeed;
public float runSpeed ;
public float walkSpeed ;
//this will get the players rigidbody
private Rigidbody2D rb;
//this will be the jump speed of the player
public float jumpSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
private Animator anim;
void Start()
{
//at the start this will get the rigidbody2d
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
//if input horizontal is greater than zero
if (Input.GetAxisRaw("Horizontal") > 0f)
{
//we then getr the velocity at its movespeed
rb.velocity = new Vector4(walkSpeed,runSpeed, rb.velocity.y, 0f);
}
//if input less than zero
else if(Input.GetAxisRaw("Horizontal")< 0f)
{
//we then get the velocity at its -movespeed
rb.velocity = new Vector4(-walkSpeed,-runSpeed, rb.velocity.y, 0f);
}
else
{
//if input x is 0f then the player will not slide
rb.velocity = new Vector3(0f, rb.velocity.y, 0f);
}
//if we hit the jump button
if (Input.GetButtonDown("Jump") && isGrounded)
{
//then we get the y axis at the give jumpspeed and the the
rb.velocity = new Vector3(rb.velocity.x, jumpSpeed, 0f);
}
anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
anim.SetBool("Grounded", isGrounded);
}
}
Guys I am sorry for my English