Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerControllerScript : MonoBehaviour {
- [Header("Main settings")]
- public Camera mainCamera;
- public float moveSpeed;
- public float lookSensitivityX;
- public float lookSensitivityY;
- public float jumpForce;
- public float healthPoints;
- public float cameraRotLimit;
- private bool isGrounded;
- private Rigidbody myRigid;
- private Vector3 vel = Vector3.zero;
- private Vector3 rotationX = Vector3.zero;
- private float cameraRotation = 0f;
- private float currentCamRotX = 0f;
- private bool isPaused;
- private int deathHeight;
- // Use this for initialization
- void Start () {
- isPaused = true;
- myRigid = GetComponent<Rigidbody> ();
- }
- // Update is called once per frame
- void Update () {
- PlayerMovement ();
- if (Input.GetKeyDown (KeyCode.Space)) {
- myRigid.AddForce (new Vector3 (0f, jumpForce, 0f));
- }
- if (Input.GetKeyDown (KeyCode.Escape) && isPaused == false) {
- isPaused = true;
- } else if (Input.GetKeyDown (KeyCode.Escape) && isPaused == true) {
- isPaused = false;
- }
- if (isPaused) {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- } else if (!isPaused) {
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- }
- void FixedUpdate () {
- //Performs Movement based on velocity variable
- if (vel != Vector3.zero) {
- myRigid.MovePosition (myRigid.position + vel * Time.fixedDeltaTime);
- }
- //Performs the camera rotation
- myRigid.MoveRotation (myRigid.rotation * Quaternion.Euler (rotationX));
- if (mainCamera != null) {
- currentCamRotX -= cameraRotation;
- currentCamRotX = Mathf.Clamp (currentCamRotX, -cameraRotLimit, cameraRotLimit);
- mainCamera.transform.localEulerAngles = new Vector3 (currentCamRotX, 0f, 0f);
- }
- }
- void PlayerMovement () {
- //Final movement vector
- Vector3 finalVelocity = (transform.right * Input.GetAxisRaw ("Horizontal") + transform.forward * Input.GetAxisRaw ("Vertical")).normalized * moveSpeed;
- //Apply movement
- Move(finalVelocity);
- //Calculate rotation as a 3D vector (turning around the y axis)
- Vector3 rotX = new Vector3 (0f, Input.GetAxisRaw("Mouse X"), 0f) * lookSensitivityX;
- //Apply rotation
- Rotate (rotX);
- //Calculate camera rotation as a 3D vector (turning around the X axis)
- float rotY = Input.GetAxisRaw ("Mouse Y") * lookSensitivityY;
- //Apply camera rotation
- RotateCamera (rotY);
- }
- public void Move (Vector3 velocity) {
- vel = velocity;
- }
- public void Rotate (Vector3 rotX) {
- rotationX = rotX;
- }
- public void RotateCamera (float camRotY){
- cameraRotation = camRotY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement