-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
109 lines (90 loc) · 3.09 KB
/
PlayerController.cs
File metadata and controls
109 lines (90 loc) · 3.09 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
101
102
103
104
105
106
107
108
109
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private float _movementSpeed = 10f;
[SerializeField] private float _jumpForce = 5f;
[SerializeField] private GameObject _bulletPref;
[SerializeField] private float _bulletSpeed = 50f;
private Transform _player;
private Rigidbody _playerRigidbody;
private bool _isGrounded = true;
private void Awake()
{
if(_bulletPref == null)
{
Debug.LogWarning("Bullet prefab not assigned!");
enabled = false;
return;
}
if (_bulletPref.GetComponent<Rigidbody>() == null)
{
Debug.LogWarning("Bullet prefab must have a Rigidbody component!");
enabled = false;
return;
}
_player = transform;
_playerRigidbody = GetComponent<Rigidbody>();
if (_playerRigidbody == null)
{
Debug.LogError("Rigidbody required!");
enabled = false;
return;
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject bullet = Instantiate(_bulletPref, _player.position + _player.forward, _player.rotation);
//Old
//bullet.GetComponent<Rigidbody>().velocity = _player.forward * _bulletSpeed;
//New (Unity 6+)
bullet.GetComponent<Rigidbody>().linearVelocity = _player.forward * _bulletSpeed;
}
}
void FixedUpdate()
{
if (_player == null) return;
float speed = _movementSpeed;
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.LeftShift))
{
speed = _movementSpeed * 1.5f;
}
Vector3 movement = new Vector3(moveX, 0, moveZ) * speed * Time.fixedDeltaTime;
//transform.Translate(movement);
// Has problems:
// - Passing through walls at high speed
// - Incorrect collisions
_playerRigidbody.MovePosition(_playerRigidbody.position + movement);
if (_isGrounded && Input.GetKeyDown(KeyCode.Space))
{
_playerRigidbody.AddForce(Vector3.up * _jumpForce, ForceMode.Impulse);
_isGrounded = false;
}
if (movement != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(movement);
_playerRigidbody.MoveRotation(Quaternion.Slerp(_playerRigidbody.rotation, targetRotation, 10f * Time.fixedDeltaTime));
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.contacts.Length > 0)
{
foreach (var contact in collision.contacts)
{
if (contact.normal.y > 0.5f)
{
_isGrounded = true;
break;
}
}
}
}
private void OnCollisionExit(Collision collision)
{
_isGrounded = false;
}
}