Advertisement
Placido_GDD

MVC FPSController

Feb 7th, 2022 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerControl : MonoBehaviour
  6. {
  7.     public Transform player;
  8.     public Transform playerCam;
  9.     public Transform playerObj;
  10.     public float mouseSensitivity;
  11.     public bool lockCursor = true;
  12.     float cameraPitch = 0.0f;
  13.     public Vector3 camOffset;
  14.     public float walkSpeed;
  15.     public float gravValue = -13.0f;
  16.     float velocityY = 0.0f;
  17.     public float jumpMagnitude;
  18.     CharacterController controller = null;
  19.     Vector3 velocity;
  20.  
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         controller = playerObj.GetComponent<CharacterController>();
  25.         if (lockCursor)
  26.         {
  27.             Cursor.lockState = CursorLockMode.Locked;
  28.             Cursor.visible = false;
  29.         }
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update()
  34.     {
  35.         UpdateMouseLook();
  36.         PlayerMovement();
  37.         Jump();
  38.     }
  39.  
  40.     void UpdateMouseLook()
  41.     {
  42.         Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
  43.         //remember to rotate both camera and player along the Y axis
  44.         player.transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivity);
  45.         playerCam.transform.Rotate(Vector3.up * mouseDelta.x* mouseSensitivity);
  46.         playerObj.transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivity);
  47.         //only rotate the camera along x axis
  48.         cameraPitch -= mouseDelta.y * mouseSensitivity;
  49.         cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
  50.         playerCam.localEulerAngles = Vector3.right * cameraPitch;
  51.     }
  52.  
  53.     void PlayerMovement()
  54.     {
  55.         player.transform.position = playerObj.transform.position + camOffset;
  56.         playerCam.transform.position = playerObj.transform.position + camOffset;
  57.         //grab input axis for keys
  58.         Vector2 inputDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  59.         inputDir.Normalize();
  60.         //
  61.         //
  62.         velocityY += gravValue * Time.deltaTime;
  63.         velocity = (playerObj.transform.forward * inputDir.y + playerObj.transform.right * inputDir.x) * walkSpeed + Vector3.up * velocityY;
  64.         controller.Move(velocity * Time.deltaTime);
  65.     }
  66.  
  67.     void Jump()
  68.     {
  69.         if (Input.GetButtonDown("Jump") && controller.isGrounded)
  70.         {
  71.             Debug.Log("Jumping");
  72.             velocityY = Mathf.Sqrt(jumpMagnitude * -2f * gravValue);
  73.             controller.Move(velocity + new Vector3(0,velocityY,0) * Time.deltaTime);
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement