mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-29 20:22:08 +01:00
add empty project with character controller
Added empty project with camera controller and basic player controller
This commit is contained in:
31
Assets/Scripts/MouseLook.cs
Normal file
31
Assets/Scripts/MouseLook.cs
Normal 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
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/MouseLook.cs.meta
Normal file
11
Assets/Scripts/MouseLook.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 27ccf256a5b5a4841834e2cc7ef253e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
63
Assets/Scripts/PlayerMovement.cs
Normal file
63
Assets/Scripts/PlayerMovement.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private CharacterController controller;
|
||||
[SerializeField] private float speed = 12f;
|
||||
[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;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
//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;
|
||||
}
|
||||
|
||||
//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 = transform.right * x + transform.forward * z;
|
||||
//Apply move vector
|
||||
controller.Move(move * speed * Time.deltaTime);
|
||||
|
||||
//JUMP
|
||||
if(Input.GetButtonDown("Jump") && isGrounded)
|
||||
{
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
//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
|
||||
}
|
||||
|
||||
void Jump()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerMovement.cs.meta
Normal file
11
Assets/Scripts/PlayerMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fcb8e32fc55ebe9478dee06683e84d92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user