- Create a new game object
PlayerControllerFPS. - Create a empty game object child under
PlayerControllerFPScalledGroundCheck. - Drag the
MainCameraunder thePlayerControllerFPS. Reset the local positioning of the camera back to 0,0,0 if needed. - Attach the
PlayerMovementscript to thePlayerControllerFPS. - Drag the CharacterController into the properties of the
PlayerMovementscript. - Drag the
GroundCheckover to theGroundCheckproperty. - Attach the
MouseLookto theMainCamera. - Drag the
PlayerControllerFPSfrom your scene onto theplayerBodyproperty. - You should have character movement and rotation!
- Press escape to stop the camera movement. Ideally you can enable a debug menu based on this.
Last active
November 18, 2021 22:24
-
-
Save whoisryosuke/28be9309bfc3682c68cb21802c38c3e5 to your computer and use it in GitHub Desktop.
Unity - Basic 3D/FPS Character and Camera Movement + Jumping - based on Brackeyes video: https://www.youtube.com/watch?v=_QajrabyTJc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class MouseLook : MonoBehaviour | |
| { | |
| private bool mouseLock = true; | |
| public float mouseSensitivity = 100f; | |
| public Transform playerBody; | |
| float xRotation = 0f; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| Cursor.lockState = CursorLockMode.Locked; | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| // Check if we disable lock state | |
| if(Input.GetButtonDown("Cancel")) | |
| { | |
| if(mouseLock == true) | |
| { | |
| Debug.Log("Unlocking cursor"); | |
| mouseLock = false; | |
| Cursor.lockState = CursorLockMode.None; | |
| } else | |
| { | |
| Debug.Log("Locking cursor"); | |
| mouseLock = true; | |
| Cursor.lockState = CursorLockMode.Locked; | |
| } | |
| } | |
| // Only move mouse when cursor is locked | |
| // For menus and overlays to prevent movement | |
| if(mouseLock == true) | |
| { | |
| // Handle movement based on mouse | |
| float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; | |
| float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; | |
| xRotation -= mouseY; | |
| xRotation = Mathf.Clamp(xRotation, -90f, 90f); | |
| transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); | |
| playerBody.Rotate(Vector3.up * mouseX); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| [RequireComponent(typeof(CharacterController))] | |
| public class PlayerMovement : MonoBehaviour | |
| { | |
| public CharacterController controller; | |
| public float speed = 12f; | |
| public float gravity = -9.81f; | |
| public float groundDistance = 0.4f; | |
| public float jumpHeight = 3f; | |
| public Transform groundCheck; | |
| Vector3 velocity; | |
| bool isGrounded; | |
| public LayerMask groundMask; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); | |
| if (isGrounded && velocity.y < 0) | |
| { | |
| velocity.y = -2f; | |
| } | |
| float x = Input.GetAxis("Horizontal"); | |
| float z = Input.GetAxis("Vertical"); | |
| Vector3 move = transform.right * x + transform.forward * z; | |
| controller.Move(move * speed * Time.deltaTime); | |
| if (Input.GetButtonDown("Jump") && isGrounded) | |
| { | |
| velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); | |
| } | |
| velocity.y += gravity * Time.deltaTime; | |
| controller.Move(velocity * Time.deltaTime); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment