Base Rigidbody Movement + FPS counter

This commit is contained in:
DerTyp187
2021-09-29 18:47:21 +02:00
parent ed0c5cece6
commit 85f16b0697
12 changed files with 335 additions and 109 deletions

View File

@@ -0,0 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
[SerializeField] private float mouseSensitivityX = 800f;
[SerializeField] private float mouseSensitivityY = 700f;
[SerializeField] private Transform playerBody; //Transform of "First Person Player" Object
private float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;//Lock Cursor in the center
}
void Update()
{
//Input.GetAxis is based on the Unity Input settings (edit -> Project Settings -> Input Manager)
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivityX * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivityY * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f); //Kopf darf sich nicht <20>berschlagen
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);//Y Achse rotieren; Quaternion ist f<>r rotation
playerBody.Rotate(Vector3.up * mouseX); //X Achse Rotieren
}
}

View File

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

View File

@@ -0,0 +1,61 @@
using UnityEngine;
public class PlayerInteraction : MonoBehaviour
{
public float interactionDistance;
public TMPro.TextMeshProUGUI interactionText;
Camera cam;
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f));
RaycastHit hit;
bool successfulHit = false;
if (Physics.Raycast(ray, out hit, interactionDistance))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
HandleInteraction(interactable);
interactionText.text = interactable.GetDescription();
successfulHit = true;
}
}
// if we miss, hide the UI
if (!successfulHit)
{
interactionText.text = "";
}
}
void HandleInteraction(Interactable interactable)
{
switch (interactable.interactionType)
{
case Interactable.InteractionType.Click:
// interaction type is click and we clicked the button -> interact
if (Input.GetButtonDown("Interact"))
{
Debug.Log("INTERACT");
interactable.Interact();
}
break;
default:
throw new System.Exception("Unsupported type of interactable.");
}
}
}

View File

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

View File

@@ -0,0 +1,79 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Header("Speed Modifier")]
[SerializeField] private float speed = 12f;
[SerializeField] private float adjustedSpeed;
[SerializeField] private float sneakSpeed = 0.4f;
[SerializeField] private float sprintSpeed = 1.8f;
[SerializeField] private float jumpHeight = 3f;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
private Rigidbody rb;
private bool isGrounded;
private bool isSprinting = false;
private bool isSneaking = false;
Vector3 movement;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
//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
}
//Sprint
if (Input.GetButton("Sprint") && isGrounded)
{
Debug.Log("[PlayerController] Sprinting");
isSprinting = true;
adjustedSpeed *= sprintSpeed;
}
else
{
isSprinting = false;
//Sneak
if (Input.GetButton("Sneak") && isGrounded)
{
Debug.Log("[PlayerController] Sneaking");
isSprinting = true;
adjustedSpeed *= sneakSpeed;
}
else
{
isSneaking = false;
}
}
}
private void FixedUpdate()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
movement = x * adjustedSpeed * transform.right + new Vector3(0, rb.velocity.y, 0) + z * adjustedSpeed * transform.forward;
rb.velocity = movement;
adjustedSpeed = speed;
}
}

View File

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