32bit_jam_conjure/Assets/Scripts/CharacterMovement.cs
2022-10-19 22:49:02 -04:00

71 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class CharacterMovement : MonoBehaviour
{
[SerializeField] private Rigidbody rb;
[Header("Enable switches")]
[SerializeField] private bool canWalk;
[SerializeField] private bool canJump;
[Header("Movement settings")]
[SerializeField] private float movementSpeed;
[SerializeField] private float jumpPower;
[Header("Character properties")]
public bool isGrounded;
public bool isJumping;
private Vector3 rawInputMovement;
private void FixedUpdate()
{
if (canWalk || canJump)
{
if (isGrounded)
{
//The walk is actually handled here
rb.AddForce(rawInputMovement * movementSpeed, ForceMode.Impulse);
}
}
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * 1, Color.yellow);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out hit, 1f, LayerMask.GetMask("Floor")) ||
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 1f, LayerMask.GetMask("Floor")))
{
isGrounded = true;
}
else
{
rawInputMovement = new Vector3(rawInputMovement.x, 0, rawInputMovement.z);
isGrounded = false;
}
}
public void Walk(InputAction.CallbackContext value){
if (canWalk)
{
Vector2 inputMovement = value.ReadValue<Vector2>();
rawInputMovement = new Vector3(-inputMovement.x, 0, inputMovement.y);
}
}
public void Jump(InputAction.CallbackContext value)
{
if(canJump && isGrounded)
{
rb.velocity = new Vector3(0, jumpPower, 0);
}
}
}