-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
40 lines (33 loc) · 1.3 KB
/
PlayerController.cs
File metadata and controls
40 lines (33 loc) · 1.3 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
using UnityEngine;
// Controls player movement and rotation.
public class PlayerController : MonoBehaviour
{
public float speed = 5.0f; // Set player's movement speed.
public float rotationSpeed = 120.0f; // Set player's rotation speed.
public float jumpForce = 5.0f;
private Rigidbody rb; // Reference to player's Rigidbody.
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody>(); // Access player's Rigidbody.
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump")) {
rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
}
}
// Handle physics-based movement and rotation.
private void FixedUpdate()
{
// Move player based on vertical input.
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * moveVertical * speed * Time.fixedDeltaTime;
rb.MovePosition(rb.position + movement);
// Rotate player based on horizontal input.
float turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.fixedDeltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
}