67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using UnityEngine;
|
|
using Player.Interactions;
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
_controller = gameObject.GetComponent<CharacterController>();
|
|
|
|
_moveSpeed = walkSpeed;
|
|
_sprintBonus = sprintSpeed - walkSpeed;
|
|
grappleHook = gameObject.GetComponent<GrappleHook>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void Update()
|
|
{
|
|
// ground check
|
|
// isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
|
_isGrounded = _controller.isGrounded;
|
|
|
|
if (_isGrounded && playerVelocity.y < 0) playerVelocity.y = -2f;
|
|
|
|
// sprint mechanic
|
|
var sprintInput = Input.GetAxis("Sprint");
|
|
// wasd player movement
|
|
var xMove = Input.GetAxis("Horizontal");
|
|
var zMove = Input.GetAxis("Vertical");
|
|
var xzMove = (transform.right * xMove + transform.forward * zMove) * (_moveSpeed + sprintInput * _sprintBonus);
|
|
|
|
// jump
|
|
if (Input.GetButtonDown("Jump") && _isGrounded) playerVelocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
|
|
|
// gravity
|
|
playerVelocity.y += gravity * Time.deltaTime;
|
|
|
|
// force applied by grapple hook
|
|
//playerVelocity += grappleHook.PullForce(transform.position) * Time.deltaTime;
|
|
|
|
// final player move
|
|
_controller.Move((playerVelocity + xzMove) * Time.deltaTime);
|
|
|
|
//Debug.Log(controller.isGrounded);
|
|
}
|
|
|
|
#region Variables
|
|
|
|
[SerializeField] private float walkSpeed = 2f;
|
|
|
|
[SerializeField] private float sprintSpeed = 10f;
|
|
|
|
private float _moveSpeed;
|
|
private float _sprintBonus;
|
|
|
|
private CharacterController _controller;
|
|
public Vector3 playerVelocity;
|
|
|
|
[SerializeField] private float jumpHeight = 2f;
|
|
|
|
[SerializeField] private float gravity = -10f;
|
|
|
|
private bool _isGrounded;
|
|
|
|
private GrappleHook grappleHook;
|
|
|
|
#endregion
|
|
} |