├── .gitignore ├── FPS.cs ├── LICENSE ├── README.md └── _config.yml /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | 8 | # Autogenerated VS/MD solution and project files 9 | ExportedObj/ 10 | *.csproj 11 | *.unityproj 12 | *.sln 13 | *.suo 14 | *.tmp 15 | *.user 16 | *.userprefs 17 | *.pidb 18 | *.booproj 19 | *.svd 20 | 21 | 22 | # Unity3D generated meta files 23 | *.pidb.meta 24 | 25 | # Unity3D Generated File On Crash Reports 26 | sysinfo.txt 27 | 28 | # Builds 29 | *.apk 30 | *.unitypackage 31 | -------------------------------------------------------------------------------- /FPS.cs: -------------------------------------------------------------------------------- 1 | // FPS Controller 2 | // 1. Create a Parent Object like a 3D model 3 | // 2. Make the Camera the user is going to use as a child and move it to the height you wish. 4 | // 3. Attach a Rigidbody to the parent 5 | // 4. Drag the Camera into the m_Camera public variable slot in the inspector 6 | // Escape Key: Escapes the mouse lock 7 | // Mouse click after pressing escape will lock the mouse again 8 | 9 | 10 | using System.Collections; 11 | using System.Collections.Generic; 12 | using UnityEngine; 13 | 14 | [RequireComponent(typeof(Rigidbody))] 15 | public class FPS : MonoBehaviour 16 | { 17 | 18 | private float speed = 5.0f; 19 | private float m_MovX; 20 | private float m_MovY; 21 | private Vector3 m_moveHorizontal; 22 | private Vector3 m_movVertical; 23 | private Vector3 m_velocity; 24 | private Rigidbody m_Rigid; 25 | private float m_yRot; 26 | private float m_xRot; 27 | private Vector3 m_rotation; 28 | private Vector3 m_cameraRotation; 29 | private float m_lookSensitivity = 3.0f; 30 | private bool m_cursorIsLocked = true; 31 | 32 | [Header("The Camera the player looks through")] 33 | public Camera m_Camera; 34 | 35 | // Use this for initialization 36 | private void Start() 37 | { 38 | m_Rigid = GetComponent(); 39 | } 40 | 41 | // Update is called once per frame 42 | public void Update() 43 | { 44 | 45 | m_MovX = Input.GetAxis("Horizontal"); 46 | m_MovY = Input.GetAxis("Vertical"); 47 | 48 | m_moveHorizontal = transform.right * m_MovX; 49 | m_movVertical = transform.forward * m_MovY; 50 | 51 | m_velocity = (m_moveHorizontal + m_movVertical).normalized * speed; 52 | 53 | //mouse movement 54 | m_yRot = Input.GetAxisRaw("Mouse X"); 55 | m_rotation = new Vector3(0, m_yRot, 0) * m_lookSensitivity; 56 | 57 | m_xRot = Input.GetAxisRaw("Mouse Y"); 58 | m_cameraRotation = new Vector3(m_xRot, 0, 0) * m_lookSensitivity; 59 | 60 | //apply camera rotation 61 | 62 | //move the actual player here 63 | if (m_velocity != Vector3.zero) 64 | { 65 | m_Rigid.MovePosition(m_Rigid.position + m_velocity * Time.fixedDeltaTime); 66 | } 67 | 68 | if (m_rotation != Vector3.zero) 69 | { 70 | //rotate the camera of the player 71 | m_Rigid.MoveRotation(m_Rigid.rotation * Quaternion.Euler(m_rotation)); 72 | } 73 | 74 | if (m_Camera != null) 75 | { 76 | //negate this value so it rotates like a FPS not like a plane 77 | m_Camera.transform.Rotate(-m_cameraRotation); 78 | } 79 | 80 | InternalLockUpdate(); 81 | 82 | } 83 | 84 | //controls the locking and unlocking of the mouse 85 | private void InternalLockUpdate() 86 | { 87 | if (Input.GetKeyUp(KeyCode.Escape)) 88 | { 89 | m_cursorIsLocked = false; 90 | } 91 | else if (Input.GetMouseButtonUp(0)) 92 | { 93 | m_cursorIsLocked = true; 94 | } 95 | 96 | if (m_cursorIsLocked) 97 | { 98 | UnlockCursor(); 99 | } 100 | else if (!m_cursorIsLocked) 101 | { 102 | LockCursor(); 103 | } 104 | } 105 | 106 | private void UnlockCursor() 107 | { 108 | Cursor.lockState = CursorLockMode.Locked; 109 | Cursor.visible = false; 110 | } 111 | 112 | private void LockCursor() 113 | { 114 | Cursor.lockState = CursorLockMode.None; 115 | Cursor.visible = true; 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Christopher Figueroa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity-FPS-Controller 2 | 3 | This is a lightweight replacement to the FPS Controller Unity Provides 4 | 5 | The Standard FPS Controller Unity Provides has a few problems: 6 | 7 | 1. Custom Collision detection 8 | 2. Includes a bunch of script you dont need 9 | 3. Adds mobile touch support 10 | 11 | Multiple Input Support: 12 | 13 | Some people have asked how to use this for FPS Co-op games with controllers. Just replace the Input Names to what you created in your input settings. Youtube 'Unity FPS xbox controller' and you can see how its suppose to be setup in the Input settings. 14 | 15 | Note: Some people have input lag due to the Gravity for Horizontal and Vertical setting in your input settings. Turn that thing up to a high number and it will get better. 16 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman --------------------------------------------------------------------------------