Question by
RedChairTeam · May 08, 2019 at 06:24 PM ·
c#animationjump
How to set Jump animation in this script?
Hi, I'm making a 2D platformer and I already got the walking and idle animation working, but all the videos I've seen have a different way to do the jump, and can't make it work with my script (I'm really new to scripting sorry). The Jump actually needs two animations, the actual jump and the falling (since you can fall for a long time). how can I make it work with this script?
Please and thank you!
public class PlayerController : MonoBehaviour
{
public Animator animator;
public float speed;
public float jumpforce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpValue;
void Start()
{
extraJumps = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
animator.SetFloat("speed", Mathf.Abs(moveInput));
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
}
private void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpValue;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpforce;
extraJumps--;
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpforce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Comment
Answer by SahanD · May 13, 2019 at 06:32 AM
you need to play 2 different animation to do this. you can go through this video this is the basic way to control animation.
untitled.png
(11.1 kB)