0% found this document useful (0 votes)
18 views

Message

dede

Uploaded by

game.vadimka0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Message

dede

Uploaded by

game.vadimka0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Callbacks;
using UnityEngine;

public class FPSController : MonoBehaviour


{

Vector3 lastMousePosition;
[SerializeField] Vector2 mouseSensitivity;
[SerializeField] GameObject cameraObject;
[SerializeField] Vector2 cameraBounds;
[SerializeField] float movmentSpeed;
[SerializeField] float jumpStrenght;

private bool isGrounded;

Rigidbody theRB;
// lastMousePosition = Input.mousePosition;

// Start is called before the first frame update


void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
theRB = GetComponent<Rigidbody>();
}

// Update is called once per frame


void Update()
{
RotateCharacter();

MoveRB();
}

private void FixedUpdate()


{
CheckForSlope();
Jump();
}

private void RotateCharacter()


{
var mouseDelta = Vector2.zero;
mouseDelta.x += Input.GetAxis("Mouse X");
mouseDelta.y += -Input.GetAxis("Mouse Y");

transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivity.x);

cameraObject.transform.Rotate(Vector3.right * mouseDelta.y *
mouseSensitivity.y );

var cameraRotation = (cameraObject.transform.localEulerAngles.x + 180) %


360 ;
cameraObject.transform.localEulerAngles =
new Vector3(Mathf.Min(Mathf.Max(cameraRotation - 180,
cameraBounds.x), cameraBounds.y),
0, 0);
}

private void MoveTransform()


{
Vector3 moveVector = Vector3.zero;

if(Input.GetKey(KeyCode.W))
moveVector += Vector3.forward;

if(Input.GetKey(KeyCode.D))
moveVector += Vector3.right;

if(Input.GetKey(KeyCode.A))
moveVector += Vector3.left;

if(Input.GetKey(KeyCode.S))
moveVector += Vector3.back;

moveVector= moveVector.normalized;
transform.Translate(moveVector * movmentSpeed * Time.deltaTime);

return;
//Movement durch
//if(Input.GetKey(KeyCode.W))
//{
// transform.Translate(Vector3.forward);
//}
//if(Input.GetKey(KeyCode.D))
//{
// transform.Translate(Vector3.right);
//}
//if(Input.GetKey(KeyCode.A))
//{
// transform.Translate(Vector3.left);
//}
//if(Input.GetKey(KeyCode.S))
//{
// transform.Translate(Vector3.back);
//}
}

private void MoveRB()


{
Vector3 moveVector = Vector3.zero;

if(Input.GetKey(KeyCode.W))
moveVector += transform.forward;

if(Input.GetKey(KeyCode.D))
moveVector += transform.right;

if(Input.GetKey(KeyCode.A))
moveVector -= transform.right;

if(Input.GetKey(KeyCode.S))
moveVector -= transform.forward;

moveVector= moveVector.normalized;
theRB.angularVelocity = Vector3.zero;
if(moveVector == Vector3.zero)
{
var gravityVel = theRB.velocity.y;
theRB.velocity = theRB.velocity * 0.9f;
theRB.velocity = new Vector3(theRB.velocity.x, gravityVel,
theRB.velocity.z);
}else{
theRB.AddForce(moveVector * movmentSpeed, ForceMode.VelocityChange);
}

private void Jump()


{
if(!isGrounded) return;

if(Input.GetKeyDown(KeyCode.Space))
{
isGrounded = false;
theRB.AddForce(Vector3.up * jumpStrenght, ForceMode.Impulse);
}
}

private void CheckForSlope()


{
RaycastHit hit;

if(Physics.Raycast(transform.position, -Vector3.up, out hit, 1.3f) &&


theRB.velocity.y <= 0)
{
theRB.velocity = Vector3.ProjectOnPlane(theRB.velocity,
hit.normal).normalized * theRB.velocity.magnitude;
isGrounded = true;
}
}

You might also like