Skip to content

Commit add86a9

Browse files
authored
Create FirstPersonControl.cs
1 parent f9d5c7d commit add86a9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

FirstPersonControl.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)