Please use Tree to get an idea of the required tree structure. Every node in Tree can be found as an image.
In addition, custom components(scripts) are also included.
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour { | |
public float speed = 12f; | |
public float gravity = -9.81f; | |
public float jumpHeight = 3f; | |
public Transform groundCheck; | |
public float groundDistance = 0.4f; | |
public LayerMask groundMask; | |
private CharacterController characterController; | |
private Vector3 velocity = Vector3.zero; | |
private bool onGround; | |
private void Start() { | |
this.characterController = this.GetComponent<CharacterController>(); | |
// find the GroundCheck | |
foreach (Transform child in this.transform) { | |
if (child.name == "GroundCheck") { | |
// use selected groundCheck position | |
this.groundCheck = child; | |
} else { | |
// or create a new one if none is found | |
GameObject groundCheckGO = new GameObject("GroundCheck"); | |
groundCheckGO.transform.position = Vector3.down; | |
groundCheckGO.transform.SetParent(this.transform); | |
} | |
} | |
} | |
private void Update() { | |
// create a sphere, and check for collisions within the sphere | |
this.onGround = Physics.CheckSphere( | |
this.groundCheck.position, | |
this.groundDistance, | |
this.groundMask | |
); | |
if (this.onGround && this.velocity.y < 0f) { | |
// please check https://youtu.be/_QajrabyTJc?t=1206 for more info | |
this.velocity.y = -2f; | |
} | |
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); | |
Vector3 movement = ( | |
input.x * transform.right + | |
input.y * transform.up + | |
input.z * transform.forward | |
).normalized; | |
// simple movement | |
this.characterController.Move(movement * speed * Time.deltaTime); | |
// jump | |
if (Input.GetButton("Jump") && this.onGround) { | |
velocity.y = Mathf.Sqrt(-2f * this.jumpHeight * this.gravity); | |
} | |
// add the gravity | |
this.velocity.y += (this.gravity / 2) * Time.deltaTime; | |
// move character while touching ground | |
this.characterController.Move(velocity * Time.deltaTime); | |
} | |
} |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MouseLook : MonoBehaviour { | |
public float sensitivity = 100f; | |
private Transform playerBody; | |
private float xRotation = 0f; | |
private void Start() { | |
this.playerBody = this.transform.parent; | |
// hide and lock cursor position | |
Cursor.lockState = CursorLockMode.Locked; | |
} | |
private void Update() { | |
Vector2 mouseInput = new Vector2( | |
Input.GetAxis("Mouse X"), | |
Input.GetAxis("Mouse Y") | |
) * this.sensitivity * Time.deltaTime; | |
// apply y | |
this.xRotation -= mouseInput.y; | |
// clamp y | |
this.xRotation = Mathf.Clamp(this.xRotation, -90f, 90f); | |
// rotate | |
this.transform.localRotation = Quaternion.Euler(this.xRotation, 0f, 0f); | |
// rotate parent instead of camera for x | |
this.playerBody.Rotate(Vector3.up * mouseInput.x); | |
} | |
} |