Merge branch 'PlayerController-Rigidbody' into main

This commit is contained in:
DerTyp187
2021-10-01 12:36:04 +02:00
13 changed files with 391 additions and 134 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d75be92c4c2783248ad8eb12442e96c4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Force Modifier")]
[SerializeField] private float speed = 12f;
[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 float modifiedSpeed;
private bool isJumpSprinting = false;
float x;
float z;
Vector3 movement;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
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);
if (isGrounded)
{
isJumpSprinting = false;
}
}
private void MovementSpeed()
{
modifiedSpeed = speed; //RESET speed
//Sprint
if (Input.GetButton("Sprint") && isGrounded)
{
isSprinting = true;
modifiedSpeed *= sprintSpeedModifier;
}
else
{
isSprinting = false;
//Sneak
if (Input.GetButton("Sneak") && isGrounded)
{
isSneaking = true;
modifiedSpeed *= sneakSpeedModifier;
}
else
{
isSneaking = false;
}
}
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
}
}
}

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPSCounter : MonoBehaviour
{
public TMPro.TextMeshProUGUI fpsText;
public float deltaTime;
void Start()
{
fpsText = gameObject.GetComponent<TMPro.TextMeshProUGUI>();
}
void Update()
{
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
float fps = 1.0f / deltaTime;
fpsText.text = Mathf.Ceil(fps).ToString() + "FPS";
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1866e0fb446be0342a91f95d71fc2c2d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,102 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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 sneakSpeed = 0.4f;
[SerializeField] private float sprintSpeed = 1.8f;
[SerializeField] private float sprintAirSpeed = 1.4f;
[SerializeField] private float gravity = -9.81f;
[SerializeField] private float jumpHeight = 3f;
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
private Vector3 velocity;
private bool isGrounded;
private bool isSprinting = false;
private bool isSneaking = false;
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);
//Reset velocity if grounded
if (isGrounded && velocity.y < 0)
{
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;
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"))
{
move *= sneakSpeed;
isSneaking = true;
//Kommt mit character model und animations
}
else
{
isSneaking = false;
}
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);
//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
}
}