Some problems with handmade basic player controller
I tried to make this script for some basic movements and I have some problems.
Player class:
using Assets.Scripts;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float InputDelay = 0.1f;
public float VerticalMovementVelocity = 12f;
public float HorizontalMovementVelocity = 12f;
public float VerticalRotationVelocity = 12f;
public float HorizontalRotationVelocity = 12f;
public float FallingVelocity = 5f;
private Rigidbody Rigidbody;
public GameObject PlayerCamera;
private float VerticalMovementInput = 0f;
private float HorizontalMovementInput = 0f;
private float VerticalRotationInput = 0f;
private float HorizontalRotationInput = 0f;
// Start is called before the first frame update
private void Start()
{
Rigidbody = Helper.GetComponentOrThrow<Rigidbody>(this);
}
// Update is called once per frame
private void Update()
{
GetInput();
}
private void FixedUpdate()
{
Walk();
Turn();
}
private void GetInput()
{
VerticalMovementInput = Input.GetAxis("Vertical");
HorizontalMovementInput = Input.GetAxis("Horizontal");
VerticalRotationInput = Input.GetAxis("Mouse Y");
HorizontalRotationInput = Input.GetAxis("Mouse X");
}
private void Walk()
{
if (IsVerticalMovementRequested() && IsHorizontalMovementRequested())
{
ExecuteVerticalAndHorizontalMovement();
}
else if (IsVerticalMovementRequested())
{
ExecuteVerticalMovement();
}
else if (IsHorizontalMovementRequested())
{
ExecuteHorizontalMovement();
}
else
{
Rigidbody.velocity = Vector3.down * FallingVelocity;
}
}
private void ExecuteVerticalAndHorizontalMovement()
{
Rigidbody.velocity = new Vector3(HorizontalMovementInput * HorizontalMovementVelocity, -1 * FallingVelocity, VerticalMovementInput * VerticalMovementVelocity);
}
private void ExecuteVerticalMovement()
{
Rigidbody.velocity = new Vector3(0, -1 * FallingVelocity, VerticalMovementInput * VerticalMovementVelocity);
}
private void ExecuteHorizontalMovement()
{
Rigidbody.velocity = new Vector3(HorizontalMovementInput * HorizontalMovementVelocity, -1 * FallingVelocity, 0);
}
private bool IsVerticalMovementRequested()
{
return Mathf.Abs(VerticalMovementInput) > InputDelay;
}
private bool IsHorizontalMovementRequested()
{
return Mathf.Abs(HorizontalMovementInput) > InputDelay;
}
private void Turn()
{
if (IsVerticalRotationRequested() && IsHorizontalRotationRequested())
{
ExecuteVerticalAndHorizontalRotation();
}
else if (IsVerticalRotationRequested())
{
ExecuteVerticalRotation();
}
else if (IsHorizontalRotationRequested())
{
ExecuteHorizontalRotation();
}
else
{
Rigidbody.rotation = Quaternion.identity;
}
}
private void ExecuteVerticalAndHorizontalRotation()
{
ExecuteVerticalRotation();
ExecuteHorizontalRotation();
}
private void ExecuteVerticalRotation()
{
PlayerCamera.transform.Rotate((VerticalRotationInput * VerticalRotationVelocity), 0, 0);
}
private void ExecuteHorizontalRotation()
{
transform.Rotate(0, (HorizontalRotationInput * HorizontalRotationVelocity), 0);
}
private bool IsVerticalRotationRequested()
{
return Mathf.Abs(VerticalRotationInput) > InputDelay;
}
private bool IsHorizontalRotationRequested()
{
return Mathf.Abs(HorizontalRotationInput) > InputDelay;
}
}
Problems noticed:
1) If player falls on the ground, player movements become rotations
2) Even if I ask on rotations to stay with a Z equal to 0 it still changes
Thank you for helping me
Comment