mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-30 04:27:09 +01:00
Update Movement
This commit is contained in:
@@ -4,23 +4,29 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class PlayerMovement : MonoBehaviour
|
public class PlayerMovement : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("Speed Modifier")]
|
[Header("Force Modifier")]
|
||||||
[SerializeField] private float speed = 12f;
|
[SerializeField] private float speed = 12f;
|
||||||
[SerializeField] private float adjustedSpeed;
|
|
||||||
[SerializeField] private float sneakSpeed = 0.9f;
|
[SerializeField] private float sneakSpeedModifier = 0.7f;
|
||||||
[SerializeField] private float sprintSpeed = 1.1f;
|
[SerializeField] private float sprintSpeedModifier = 1.4f;
|
||||||
[SerializeField] private float jumpHeight = 3.5f;
|
[SerializeField] private float jumpSprintSpeedModifier = 20f;
|
||||||
|
[SerializeField] private float jumpForce = 3.5f;
|
||||||
|
[SerializeField] private float fallMultiplier = 2.5f;
|
||||||
|
|
||||||
[Header("Ground Check")]
|
[Header("Ground Check")]
|
||||||
[SerializeField] private Transform groundCheck;
|
[SerializeField] private Transform groundCheck;
|
||||||
[SerializeField] private float groundDistance = 0.4f;
|
[SerializeField] private float groundDistance = 0.4f;
|
||||||
[SerializeField] private LayerMask groundMask;
|
[SerializeField] private LayerMask groundMask;
|
||||||
|
|
||||||
|
public bool isSprinting = false;
|
||||||
|
public bool isSneaking = false;
|
||||||
|
|
||||||
private Rigidbody rb;
|
private Rigidbody rb;
|
||||||
private bool isGrounded;
|
private bool isGrounded;
|
||||||
private bool isSprinting = false;
|
private float modifiedSpeed;
|
||||||
private bool isSneaking = false;
|
float x;
|
||||||
|
float z;
|
||||||
|
|
||||||
Vector3 movement;
|
Vector3 movement;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
@@ -29,24 +35,33 @@ public class PlayerMovement : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
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
|
//Check every frame if the player stands on the ground
|
||||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
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
|
//Sprint
|
||||||
if (Input.GetButton("Sprint") && isGrounded)
|
if (Input.GetButton("Sprint") && isGrounded)
|
||||||
{
|
{
|
||||||
Debug.Log("[PlayerController] Sprinting");
|
|
||||||
isSprinting = true;
|
isSprinting = true;
|
||||||
adjustedSpeed *= sprintSpeed;
|
modifiedSpeed *= sprintSpeedModifier;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -54,26 +69,41 @@ public class PlayerMovement : MonoBehaviour
|
|||||||
//Sneak
|
//Sneak
|
||||||
if (Input.GetButton("Sneak") && isGrounded)
|
if (Input.GetButton("Sneak") && isGrounded)
|
||||||
{
|
{
|
||||||
Debug.Log("[PlayerController] Sneaking");
|
isSneaking = true;
|
||||||
isSprinting = true;
|
modifiedSpeed *= sneakSpeedModifier;
|
||||||
adjustedSpeed *= sneakSpeed;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
isSneaking = false;
|
isSneaking = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void FixedUpdate()
|
//Sprint jump
|
||||||
|
if (isSprinting && Input.GetButtonDown("Jump"))
|
||||||
{
|
{
|
||||||
float x = Input.GetAxis("Horizontal");
|
Debug.Log("Jump Sprint");
|
||||||
float z = Input.GetAxis("Vertical");
|
rb.AddRelativeForce(Vector3.forward * jumpSprintSpeedModifier);
|
||||||
|
|
||||||
|
|
||||||
movement = x * adjustedSpeed * transform.right + new Vector3(0, rb.velocity.y, 0) + z * adjustedSpeed * transform.forward;
|
|
||||||
rb.velocity = movement;
|
|
||||||
adjustedSpeed = speed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 Jump()
|
||||||
|
{
|
||||||
|
//Better Falling
|
||||||
|
if(rb.velocity.y < 0)
|
||||||
|
{
|
||||||
|
rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user