Skip to content

Commit 4abea8d

Browse files
Core
1 parent 3254d32 commit 4abea8d

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

FreeLook.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class FreeLook : MonoBehaviour
6+
{
7+
new Camera camera;
8+
9+
public float movementSpeed = 10.0f;
10+
public float turnSpeed = 3.0f;
11+
public Vector3 cameraSpawn;
12+
public bool cursorVisible = false;
13+
public bool lockCursor = true;
14+
15+
float yaw = 0.0f;
16+
float pitch = 0.0f;
17+
float turnSpeedH;
18+
float turnSpeedV;
19+
float inputH = 0.0f;
20+
float inputV = 0.0f;
21+
bool buttonJumpDown = false;
22+
23+
void Start()
24+
{
25+
camera = GetComponent<Camera>();
26+
27+
// Camera object check
28+
if(camera == null)
29+
{
30+
Debug.LogError("FreeLook: No camera component was found on this gameobject.");
31+
}
32+
33+
// Camera spawn
34+
if (cameraSpawn == null)
35+
{
36+
cameraSpawn = transform.position;
37+
}
38+
39+
transform.position = cameraSpawn;
40+
41+
// Turn speed
42+
turnSpeedH = turnSpeed;
43+
turnSpeedV = turnSpeed;
44+
45+
// Cursor
46+
CursorSettings();
47+
}
48+
49+
void Update()
50+
{
51+
yaw += turnSpeedH * Input.GetAxis("Mouse X");
52+
pitch -= turnSpeedV * Input.GetAxis("Mouse Y");
53+
54+
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
55+
56+
inputH = Input.GetAxis("Horizontal");
57+
inputV = Input.GetAxis("Vertical");
58+
59+
Debug.Log(inputH);
60+
Debug.Log(inputV);
61+
62+
Vector3 moveDirection = (transform.forward * inputV + inputH * transform.right).normalized;
63+
64+
if(Input.GetKey(KeyCode.X))
65+
{
66+
moveDirection.y = -1.0f;
67+
}
68+
69+
if(Input.GetButton("Jump"))
70+
{
71+
moveDirection.y = 1.0f;
72+
}
73+
74+
transform.position = transform.position + moveDirection * movementSpeed * Time.deltaTime;
75+
}
76+
77+
private void OnApplicationFocus(bool focus)
78+
{
79+
if(focus)
80+
{
81+
CursorSettings();
82+
}
83+
}
84+
85+
private void CursorSettings()
86+
{
87+
// Cursor
88+
Cursor.visible = cursorVisible;
89+
90+
if (lockCursor)
91+
{
92+
Cursor.lockState = CursorLockMode.Locked;
93+
}
94+
else
95+
{
96+
Cursor.lockState = CursorLockMode.None;
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)