Update Movement

This commit is contained in:
DerTyp187
2021-09-29 19:57:09 +02:00
parent f7cd996097
commit d211c0468e

View File

@@ -4,23 +4,29 @@ using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Speed Modifier")]
[Header("Force Modifier")]
[SerializeField] private float speed = 12f;
[SerializeField] private float adjustedSpeed;
[SerializeField] private float sneakSpeed = 0.9f;
[SerializeField] private float sprintSpeed = 1.1f;
[SerializeField] private float jumpHeight = 3.5f;
[SerializeField] private float sneakSpeedModifier = 0.7f;
[SerializeField] private float sprintSpeedModifier = 1.4f;
[SerializeField] private float jumpSprintSpeedModifier = 20f;
[SerializeField] private float jumpForce = 3.5f;
[SerializeField] private float fallMultiplier = 2.5f;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
public bool isSprinting = false;
public bool isSneaking = false;
private Rigidbody rb;
private bool isGrounded;
private bool isSprinting = false;
private bool isSneaking = false;
private float modifiedSpeed;
float x;
float z;
Vector3 movement;
private void Start()
@@ -29,24 +35,33 @@ public class PlayerMovement : MonoBehaviour
}
private void Update()
{
Grounded();
Jump();
MovementSpeed();
}
private void FixedUpdate()
{
rb.velocity = movement;
}
private void Grounded()
{
//Check every frame if the player stands on the ground
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
//JUMP
if(Input.GetButtonDown("Jump") && isGrounded)
{
//Add Force Up To Jump
rb.AddForce(Vector3.up * jumpHeight * 100); //Times 100 -> so we can use smaller numbers in the editor
}
}
private void MovementSpeed()
{
modifiedSpeed = speed; //RESET speed
//Sprint
if (Input.GetButton("Sprint") && isGrounded)
{
Debug.Log("[PlayerController] Sprinting");
isSprinting = true;
adjustedSpeed *= sprintSpeed;
modifiedSpeed *= sprintSpeedModifier;
}
else
{
@@ -54,26 +69,41 @@ public class PlayerMovement : MonoBehaviour
//Sneak
if (Input.GetButton("Sneak") && isGrounded)
{
Debug.Log("[PlayerController] Sneaking");
isSprinting = true;
adjustedSpeed *= sneakSpeed;
isSneaking = true;
modifiedSpeed *= sneakSpeedModifier;
}
else
{
isSneaking = false;
}
}
}
//Sprint jump
if (isSprinting && Input.GetButtonDown("Jump"))
{
Debug.Log("Jump Sprint");
rb.AddRelativeForce(Vector3.forward * jumpSprintSpeedModifier);
}
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
movement = x * modifiedSpeed * transform.right + new Vector3(0, rb.velocity.y, 0) + z * modifiedSpeed * transform.forward;
}
private void FixedUpdate()
private void Jump()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Better Falling
if(rb.velocity.y < 0)
{
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
}
movement = x * adjustedSpeed * transform.right + new Vector3(0, rb.velocity.y, 0) + z * adjustedSpeed * transform.forward;
rb.velocity = movement;
adjustedSpeed = speed;
if (Input.GetButtonDown("Jump") && isGrounded)
{
//Add Force Up To Jump
rb.AddForce(Vector3.up * jumpForce * 100); //Times 100 -> so we can use smaller numbers in the editor
}
}
}