File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ using System . Collections ;
2+ using System . Collections . Generic ;
3+ using UnityEngine ;
4+
5+ public class FirstPersonControl : MonoBehaviour {
6+
7+ public GameObject player ;
8+
9+ private bool inMouseMode = false ;
10+ private float rotationX ;
11+ private float rotationY ;
12+
13+ void Start ( )
14+ {
15+ Cursor . lockState = CursorLockMode . Locked ;
16+ Cursor . visible = false ;
17+ }
18+
19+ void Update ( )
20+ {
21+ if ( Input . GetKeyDown ( KeyCode . Space ) ) {
22+ Cursor . lockState = CursorLockMode . None ;
23+ Cursor . visible = true ;
24+ inMouseMode = true ;
25+ }
26+ if ( Input . GetKeyUp ( KeyCode . Space ) ) {
27+ Cursor . lockState = CursorLockMode . Locked ;
28+ Cursor . visible = false ;
29+ inMouseMode = false ;
30+ }
31+
32+ if ( ! inMouseMode ) {
33+ player . transform . Translate ( Input . GetAxis ( "Horizontal" ) * Time . deltaTime * 10f , 0f , 0f ) ;
34+ player . transform . Translate ( 0f , 0f , Input . GetAxis ( "Vertical" ) * Time . deltaTime * 10f ) ;
35+ rotationY += transform . localEulerAngles . y + Input . GetAxis ( "Mouse X" ) * 2.0f ;
36+ rotationX += Input . GetAxis ( "Mouse Y" ) * 2.0f ;
37+ Camera . main . transform . localEulerAngles = new Vector3 ( - rotationX , 0f , 0f ) ;
38+ player . transform . localEulerAngles = new Vector3 ( 0f , rotationY , 0f ) ;
39+ }
40+ }
41+
42+ }
You can’t perform that action at this time.
0 commit comments