-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShipController.cs
More file actions
100 lines (78 loc) · 2.99 KB
/
ShipController.cs
File metadata and controls
100 lines (78 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipController : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] Transform PlayerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] bool LockCursor = true;
[SerializeField] float MovementSpeed = 6.0f;
[SerializeField] float Gravity = -13.0f;
[SerializeField] [Range(0.0f, 0.5f)] float MoveSmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float MouseSmoothTime = 0.03f;
[SerializeField] bool canmove = true;
float CameraPitch = 0.0f;
float VelocityY = 0.0f;
CharacterController Controller = null;
Vector2 CurrentDirection = Vector2.zero;
Vector2 CurrentDirVelocity = Vector2.zero;
Vector2 CurrentMouseDelta = Vector2.zero;
Vector2 CurrentMouseDeltaVelocity = Vector2.zero;
public void Start()
{
this.GetComponent<ShipController>().enabled = false;
canmove = true;
Controller = GetComponent<CharacterController>();
if (LockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
public void stop()
{
canmove = false;
}
public void unlock()
{
Controller = GetComponent<CharacterController>();
if (LockCursor)
{
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = true;
}
}
// Update is called once per frame
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void UpdateMouseLook()
{
if (canmove == true)
{
Vector2 TargetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
CurrentMouseDelta = Vector2.SmoothDamp(CurrentMouseDelta, TargetMouseDelta, ref CurrentMouseDeltaVelocity, MouseSmoothTime);
CameraPitch -= CurrentMouseDelta.y * mouseSensitivity;
CameraPitch = Mathf.Clamp(CameraPitch, -75.0f, 75.0f);
PlayerCamera.localEulerAngles = Vector3.right * CameraPitch;
transform.Rotate(Vector3.up * CurrentMouseDelta.x * mouseSensitivity);
}
}
void UpdateMovement()
{
if (canmove == true)
{
Vector2 TargetDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
TargetDirection.Normalize();
CurrentDirection = Vector2.SmoothDamp(CurrentDirection, TargetDirection, ref CurrentDirVelocity, MoveSmoothTime);
if (Controller.isGrounded)
VelocityY = 0.0f;
VelocityY += Gravity * Time.deltaTime;
Vector3 Velocity = (transform.forward * CurrentDirection.y + transform.right * CurrentDirection.x) * MovementSpeed + Vector3.up * VelocityY;
Controller.Move(Velocity * Time.deltaTime);
}
}
}