Fix slope and character rotation on slopes
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Scampz.GameJam.Assets.Scripts.Player;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Scampz.GameJam.Assets.Scripts
|
||||
{
|
||||
@@ -8,29 +7,60 @@ namespace Scampz.GameJam.Assets.Scripts
|
||||
private CharacterController _controller;
|
||||
public float Speed = 10f;
|
||||
public float RotateSpeed = 720.0f;
|
||||
private PlayerState _playerState;
|
||||
private float _ySpeed = 0f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_playerState = GetComponent<PlayerState>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
//Physics.Raycast(transform.position, -transform.up, out var hit, 10f);
|
||||
//var slope = hit.normal.y;
|
||||
var direction = new Vector3(Input.GetAxisRaw(InputOptions.Horizontal), 0, Input.GetAxisRaw(InputOptions.Vertical)).normalized;
|
||||
Move();
|
||||
}
|
||||
|
||||
if (_playerState.CanMove)
|
||||
_controller.SimpleMove(-direction * Speed);
|
||||
void Move()
|
||||
{
|
||||
var horizontalMovement = Input.GetAxisRaw(InputOptions.Horizontal);
|
||||
var verticalMovement = Input.GetAxisRaw(InputOptions.Vertical);
|
||||
|
||||
if (direction != Vector3.zero)
|
||||
var movementDirection = new Vector3(-horizontalMovement, 0, -verticalMovement);
|
||||
var magnitude = Mathf.Clamp01(movementDirection.magnitude) * Speed;
|
||||
movementDirection.Normalize();
|
||||
_ySpeed += Physics.gravity.y * Time.deltaTime;
|
||||
|
||||
if (_controller.isGrounded)
|
||||
_ySpeed -= 0.5f;
|
||||
|
||||
var velocity = movementDirection * magnitude;
|
||||
velocity = AdjustVelocityToSlope(velocity);
|
||||
velocity.y += _ySpeed;
|
||||
|
||||
_controller.Move(velocity * Time.deltaTime);
|
||||
|
||||
if (movementDirection != Vector3.zero)
|
||||
{
|
||||
var toRotation = Quaternion.LookRotation(-direction, Vector3.up);
|
||||
|
||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, RotateSpeed);
|
||||
var ray = new Ray(transform.position, Vector3.down);
|
||||
Physics.Raycast(ray, out var hitInfo, 5f);
|
||||
var toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
|
||||
toRotation.x = Quaternion.LookRotation(Vector3.Cross(transform.right, hitInfo.normal)).x;
|
||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, RotateSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 AdjustVelocityToSlope(Vector3 velocity)
|
||||
{
|
||||
var ray = new Ray(transform.position, Vector3.down);
|
||||
|
||||
if (Physics.Raycast(ray, out var hitInfo, 5f))
|
||||
{
|
||||
var slopeRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
|
||||
var adjustedVelocity = slopeRotation * velocity;
|
||||
if (adjustedVelocity.y < 0)
|
||||
return adjustedVelocity;
|
||||
}
|
||||
|
||||
return velocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user