!!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private float horizontal;
private float speed = 12f;
private float jumpPower = 17.5f;
private int jumpCount = 0;
private bool isGrounded = false;
private SpriteRenderer spriteRenderer;
private Animator animator;
[SerializeField] private Rigidbody2D rb;
private void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
Flip();
DoubleJump();
}
private void FixedUpdate()
{
Movement();
}
private void Flip()
{
if (horizontal < 0f)
{
spriteRenderer.flipX = true;
}
else if (horizontal > 0f)
{
spriteRenderer.flipX = false;
}
}
private void Movement()
{
animator.SetFloat("yVelocity", rb.velocity.y);
animator.SetFloat("xVelocity", Math.Abs(rb.velocity.x));
// Get Value of -1 and 1
horizontal = Input.GetAxisRaw("Horizontal");
//Makes the player move left and right
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
private void DoubleJump()
{
if (isGrounded)
{
jumpCount = 0;
}
if (Input.GetButtonDown("Jump") && jumpCount < 2)
{
isGrounded = false;
Jump();
}
}
private void Jump()
{
animator.SetBool("isJumping", !isGrounded);
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
jumpCount++;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
Debug.Log("Ground Detected");
isGrounded = true;
animator.SetBool("isJumping", !isGrounded);
}
}
}