216 lines
6.2 KiB
C#
216 lines
6.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
[SerializeField] private float speed = 10f;
|
|
[SerializeField] private float jumpPower = 1.4f;
|
|
|
|
|
|
private float wallJumpX = 1500; //Horizontal wall jump force
|
|
private float wallJumpY = 750; //Vertical wall jump force
|
|
|
|
[Header("Wallmovement")]
|
|
[SerializeField] private float coyoteTime; //How much time the player can hang in the air before jumping
|
|
private float coyoteCounter; //How much time passed since the player ran off the edge
|
|
|
|
public float wallSlideSpeed = 2;
|
|
public Vector2 wallJumpPower = new Vector2(10f, 10f);
|
|
public Vector2 wallClimbPower = new Vector2(5f, 10f);
|
|
|
|
[Header("Layers")]
|
|
[SerializeField] private LayerMask groundLayer;
|
|
[SerializeField] private LayerMask wallLayer;
|
|
|
|
private Rigidbody2D body;
|
|
private Animator animator;
|
|
private BoxCollider2D boxCollider2D;
|
|
//private bool grounded;
|
|
private Vector2 moveInput;
|
|
private float wallJumpCooldown;
|
|
|
|
//Wall Jumping
|
|
//bool isWallJumping;
|
|
//float wallJumpDirection;
|
|
//float wallJumpTime = 0.5f;
|
|
//float wallJumpTimer;
|
|
|
|
//private float wallJumpLockTime = 0.15f;
|
|
//private float wallJumpLockCounter;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
body = GetComponent<Rigidbody2D>();
|
|
animator = GetComponent<Animator>();
|
|
boxCollider2D = GetComponent<BoxCollider2D>();
|
|
}
|
|
|
|
|
|
private void Update()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void FixedUpdate()
|
|
{
|
|
float horizontalInput = moveInput.x;
|
|
//body.linearVelocity = new Vector2(moveInput.x * speed, body.linearVelocityY);
|
|
|
|
//Flip player right/left movement
|
|
if (horizontalInput > 0.01f)
|
|
{
|
|
transform.localScale = Vector3.one;
|
|
} else if (horizontalInput < -0.01f)
|
|
{
|
|
transform.localScale = new Vector3(-1, 1, 1);
|
|
}
|
|
|
|
if (OnWall())
|
|
{
|
|
if (moveInput.x != 0)
|
|
{
|
|
//body.gravityScale = 0;
|
|
//body.linearVelocity = Vector2.zero;
|
|
WallSlide();
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
//body.gravityScale = 7;
|
|
if (moveInput.x != 0)
|
|
body.linearVelocity = new Vector2(horizontalInput * speed, body.linearVelocity.y);
|
|
//else
|
|
//{
|
|
// body.linearVelocity = new Vector2(horizontalInput * speed, body.linearVelocity.y);
|
|
//}
|
|
if (IsGrounded())
|
|
{
|
|
coyoteCounter = coyoteTime; //Reset coyote counter when on the ground
|
|
body.linearVelocity = new Vector2(moveInput.x * speed, body.linearVelocityY);
|
|
}
|
|
|
|
else
|
|
coyoteCounter -= Time.deltaTime; //Start decreasing coyote counter when not on the ground
|
|
}
|
|
|
|
|
|
|
|
//Set abunatir parameters
|
|
animator.SetBool("run", horizontalInput != 0);
|
|
animator.SetBool("grounded", IsGrounded());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Jump(InputAction.CallbackContext callback)
|
|
{
|
|
if (coyoteCounter <= 0 && !OnWall() ) return;
|
|
//Debug.Log($"Jumping {callback.performed} - Is Grounded {IsGrounded()}");
|
|
//if (callback.performed && IsGrounded())
|
|
if (callback.performed)
|
|
{
|
|
if (OnWall() && !IsGrounded())
|
|
{
|
|
if (moveInput.x == 0)
|
|
{
|
|
WallJump();
|
|
} else
|
|
{
|
|
WallClimb();
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if (IsGrounded())
|
|
{
|
|
Debug.Log("We are supposed to jump");
|
|
body.linearVelocity = new Vector2(body.linearVelocityX, speed * jumpPower);
|
|
animator.SetTrigger("jump");
|
|
}
|
|
else
|
|
{
|
|
if (coyoteCounter > 0)
|
|
body.linearVelocity = new Vector2(body.linearVelocity.x, jumpPower);
|
|
}
|
|
|
|
//Reset coyote counter to 0 to avoid double jumps
|
|
coyoteCounter = 0;
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public void Move(InputAction.CallbackContext callback)
|
|
{
|
|
moveInput = callback.ReadValue<Vector2>();
|
|
//Debug.Log("Move Input " + moveInput);
|
|
}
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
{
|
|
//if(collision.gameObject.tag == "Ground")
|
|
//{
|
|
// grounded = true;
|
|
//}
|
|
}
|
|
|
|
private void WallSlide()
|
|
{
|
|
//if (OnWall() )
|
|
//{
|
|
// body.linearVelocity = new Vector2(body.linearVelocityX, Mathf.Max(body.linearVelocity.y, -wallSlideSpeed));
|
|
|
|
//}
|
|
Debug.Log("Wallsliding");
|
|
|
|
body.linearVelocity = new Vector2(body.linearVelocityX, Mathf.Max(body.linearVelocity.y, -wallSlideSpeed));
|
|
}
|
|
|
|
private void WallJump()
|
|
{
|
|
//Climb
|
|
//body.AddForce(new Vector2(-Mathf.Sign(transform.localScale.x) * wallJumpX, wallJumpY), ForceMode2D.Impulse);
|
|
Debug.Log("WallJump");
|
|
//Jump
|
|
body.linearVelocity = new Vector2(-Mathf.Sign(transform.localScale.x) * wallJumpPower.x, wallJumpPower.y);
|
|
wallJumpCooldown = 0;
|
|
}
|
|
|
|
private void WallClimb()
|
|
{
|
|
//Climb
|
|
//body.AddForce(new Vector2(-Mathf.Sign(transform.localScale.x) * wallJumpX, wallJumpY));
|
|
Debug.Log("WallClimb");
|
|
//Jump
|
|
body.linearVelocity = new Vector2(-Mathf.Sign(transform.localScale.x) * wallClimbPower.x, wallClimbPower.y);
|
|
wallJumpCooldown = 0;
|
|
}
|
|
|
|
private bool IsGrounded()
|
|
{
|
|
RaycastHit2D hit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
|
|
return hit.collider != null;
|
|
}
|
|
|
|
private bool OnWall()
|
|
{
|
|
|
|
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
|
|
return raycastHit.collider != null;
|
|
}
|
|
}
|