2024-06-12 13:00:57 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class PlayerMoveScript : MonoBehaviour
|
|
|
|
{
|
|
|
|
|
|
|
|
public float speed;
|
|
|
|
public float xSensitivity;
|
|
|
|
public float ySensitivity;
|
|
|
|
private Rigidbody rb;
|
2024-06-13 12:42:36 +02:00
|
|
|
public Camera cam;
|
2024-06-12 13:00:57 +02:00
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
// 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-13 12:42:36 +02:00
|
|
|
look();
|
|
|
|
|
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;
|
|
|
|
float newDirY = currentRotation.x - yMouseMovement;
|
2024-06-12 13:00:57 +02:00
|
|
|
|
2024-06-13 12:42:36 +02:00
|
|
|
cam.transform.rotation = Quaternion.Euler(0, newDirX, 0);
|
|
|
|
transform.rotation = Quaternion.Euler(newDirY, 0, 0);
|
2024-06-12 13:00:57 +02:00
|
|
|
}
|
|
|
|
}
|