2024-06-12 13:00:57 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-06-14 10:30:48 +02:00
|
|
|
using System.Threading;
|
2024-06-14 11:38:49 +02:00
|
|
|
using Unity.VisualScripting;
|
2024-06-12 13:00:57 +02:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class PlayerMoveScript : MonoBehaviour
|
|
|
|
{
|
|
|
|
|
2024-06-14 11:38:49 +02:00
|
|
|
[Header("Movement")]
|
|
|
|
[SerializeField] private float speed;
|
|
|
|
[SerializeField] private float jumpForce;
|
2024-07-03 12:38:08 +02:00
|
|
|
[SerializeField] private float jumpPointDistance;
|
|
|
|
[SerializeField] private float jumpPointRadius;
|
|
|
|
|
2024-06-14 11:38:49 +02:00
|
|
|
[Header("Looking")]
|
|
|
|
[SerializeField] private float xSensitivity;
|
|
|
|
[SerializeField] private float ySensitivity;
|
|
|
|
[SerializeField] private Camera cam;
|
2024-06-12 13:00:57 +02:00
|
|
|
private Rigidbody rb;
|
|
|
|
|
2024-06-14 10:30:48 +02:00
|
|
|
private bool cursorIsLocked;
|
2024-07-03 12:38:08 +02:00
|
|
|
private LayerMask ground;
|
2024-06-14 10:30:48 +02:00
|
|
|
|
2024-06-12 13:00:57 +02:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody>();
|
2024-06-14 10:30:48 +02:00
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
cursorIsLocked = true;
|
2024-06-19 09:11:15 +02:00
|
|
|
|
2024-07-03 12:38:08 +02:00
|
|
|
ground = LayerMask.GetMask("Ground");
|
2024-06-12 13:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
2024-06-14 11:38:49 +02:00
|
|
|
{
|
2024-06-19 12:55:59 +02:00
|
|
|
jump();
|
2024-06-14 11:38:49 +02:00
|
|
|
move();
|
|
|
|
look();
|
|
|
|
lockCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void move()
|
2024-06-12 13:00:57 +02:00
|
|
|
{
|
|
|
|
// In welche Richtung rennt der Spieler?
|
|
|
|
int moveDirX = 0;;
|
|
|
|
int moveDirZ = 0;
|
|
|
|
|
2024-06-13 12:42:36 +02:00
|
|
|
if(Input.GetKey(KeyCode.W)){moveDirZ++;}
|
|
|
|
if(Input.GetKey(KeyCode.S)){moveDirZ--;}
|
|
|
|
if(Input.GetKey(KeyCode.D)){moveDirX++;}
|
|
|
|
if(Input.GetKey(KeyCode.A)){moveDirX--;}
|
2024-06-12 13:00:57 +02:00
|
|
|
|
|
|
|
Vector3 moveDir = new Vector3(moveDirX, 0, moveDirZ);
|
|
|
|
moveDir.Normalize();
|
|
|
|
|
|
|
|
rb.velocity = transform.TransformDirection(new Vector3(moveDir.x * speed, rb.velocity.y, moveDir.z * speed));
|
2024-06-14 11:38:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void jump()
|
|
|
|
{
|
2024-06-19 12:55:59 +02:00
|
|
|
if(Input.GetKey(KeyCode.Space))
|
2024-06-14 11:38:49 +02:00
|
|
|
{
|
2024-07-03 12:38:08 +02:00
|
|
|
if (Physics.CheckSphere(transform.position + Vector3.down * jumpPointDistance, jumpPointRadius, ground) )
|
2024-06-14 11:38:49 +02:00
|
|
|
{
|
2024-06-19 12:55:59 +02:00
|
|
|
rb.isKinematic = true;
|
|
|
|
rb.isKinematic = false;
|
2024-06-14 11:38:49 +02:00
|
|
|
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 13:00:57 +02:00
|
|
|
|
2024-06-14 11:38:49 +02:00
|
|
|
}
|
2024-06-14 10:30:48 +02:00
|
|
|
|
2024-06-14 11:38:49 +02:00
|
|
|
void OnDrawGizmosSelected()
|
|
|
|
{
|
|
|
|
// Draws a Line for checkForJump
|
|
|
|
Gizmos.color = Color.red;
|
2024-07-03 12:38:08 +02:00
|
|
|
Vector3 direction = transform.TransformDirection(Vector3.down) * jumpPointDistance;
|
2024-06-14 11:38:49 +02:00
|
|
|
Gizmos.DrawRay(transform.position, direction);
|
2024-07-03 12:38:08 +02:00
|
|
|
|
|
|
|
// Draw a yellow sphere at the transform's position
|
|
|
|
Gizmos.color = Color.blue;
|
|
|
|
Gizmos.DrawWireSphere(transform.position + Vector3.down * jumpPointDistance, jumpPointRadius);
|
|
|
|
|
|
|
|
|
2024-06-12 13:00:57 +02:00
|
|
|
}
|
|
|
|
|
2024-06-19 09:11:15 +02:00
|
|
|
public float newDirY;
|
2024-06-12 13:00:57 +02:00
|
|
|
public void look()
|
|
|
|
{
|
2024-06-13 12:42:36 +02:00
|
|
|
float xMouseMovement = Input.GetAxis("Mouse X") * xSensitivity * Time.deltaTime;
|
|
|
|
float yMouseMovement = Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
|
|
|
|
|
|
|
|
Vector3 currentRotation = cam.transform.rotation.eulerAngles;
|
2024-06-12 13:00:57 +02:00
|
|
|
|
2024-06-13 12:42:36 +02:00
|
|
|
float newDirX = currentRotation.y + xMouseMovement;
|
2024-06-19 09:11:15 +02:00
|
|
|
newDirY = newDirY - yMouseMovement;
|
|
|
|
newDirY = Mathf.Clamp(newDirY, -90f, 90f);
|
2024-06-12 13:00:57 +02:00
|
|
|
|
2024-06-14 10:30:48 +02:00
|
|
|
cam.transform.localRotation = Quaternion.Euler(newDirY, 0, 0);
|
2024-06-19 09:11:15 +02:00
|
|
|
transform.localRotation = Quaternion.Euler(0, newDirX, 0);
|
2024-06-14 10:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void lockCursor()
|
|
|
|
{
|
|
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
|
|
{
|
|
|
|
if(!cursorIsLocked)
|
|
|
|
{
|
|
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
|
|
cursorIsLocked = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Cursor.lockState = CursorLockMode.None;
|
|
|
|
cursorIsLocked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-06-12 13:00:57 +02:00
|
|
|
}
|
|
|
|
}
|