basic air control and sprinting

This commit is contained in:
DerTyp187
2021-09-21 18:34:02 +02:00
parent 61294cb761
commit e3064f0823
4 changed files with 72 additions and 16 deletions

View File

@@ -6,7 +6,11 @@ public class PlayerMovement : MonoBehaviour
{
[SerializeField] private CharacterController controller;
//[SerializeField] private Transform playerTransform;
[SerializeField] private float speed = 12f;
[SerializeField] private float airSpeed = 0.6f;
[SerializeField] private float sprintSpeed = 1.8f;
[SerializeField] private float sprintAirSpeed = 1.2f;
[SerializeField] private float gravity = -9.81f;
[SerializeField] private float jumpHeight = 3f;
@@ -16,6 +20,7 @@ public class PlayerMovement : MonoBehaviour
private Vector3 velocity;
private bool isGrounded;
private bool isSprinting;
void Start()
{
@@ -24,7 +29,8 @@ public class PlayerMovement : MonoBehaviour
void Update()
{
{
//GROUND CHECK
//Creates an invisible sphere on the bottom of our player
//And checks if it's colliding with something !with the "ground"-Mask in Unity!
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
@@ -34,30 +40,60 @@ public class PlayerMovement : MonoBehaviour
{
velocity.y = -2f;
}
//MOVEMENT
//Input.GetAxis is based on the Unity Input settings (edit -> Project Settings -> Input Manager)
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Create move vector !in look direction!
Vector3 move = transform.right * x + transform.forward * z;
Vector3 move;
if (isGrounded)//for air control
{
move = transform.right * x + transform.forward * z;
//SPRINT
if (Input.GetButton("Sprint"))
{
move *= sprintSpeed;
isSprinting = true;
}
else
{
//SNEAK
if (Input.GetButton("Sneak"))
{
//Kommt mit character model und animations
}
isSprinting = false;
}
//JUMP
if (Input.GetButtonDown("Jump"))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
else//Air control
{
if (isSprinting)
{
move = transform.right * x * airSpeed * sprintAirSpeed + transform.forward * z * airSpeed * sprintAirSpeed;
}
else
{
move = transform.right * x * airSpeed + transform.forward * z * airSpeed;
}
}
//Apply move vector
controller.Move(move * speed * Time.deltaTime);
//JUMP
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//Add gravity to current velocity
velocity.y += gravity * Time.deltaTime;
//apply gravity
controller.Move(velocity * Time.deltaTime);//nochmal time.deltatime wegen irgendwas mit physikalischer Formel und so
}
void Jump()
{
}
}