-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerController.cs
68 lines (56 loc) · 1.83 KB
/
PlayerController.cs
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
using Mirror;
using UnityEngine;
public class PlayerController : NetworkBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 4f;
public Camera camera;
private Rigidbody rb;
// Adjust this value to control mouse sensitivity
public float sensitivity = 0.01f;
private float previousX = 0.0f;
private float previousY = 0.0f;
[Client]
void Start()
{
if (isLocalPlayer)
{
rb = GetComponent<Rigidbody>();
camera.gameObject.SetActive(true);
} else
{
camera.gameObject.SetActive(false);
}
}
[Client]
void Update()
{
if (isLocalPlayer)
{
// Handle player input
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Calculate the movement vector in local space
Vector3 input = new Vector3(horizontalInput, 0f, verticalInput);
Vector3 movement = transform.TransformDirection(input.normalized) * moveSpeed * Time.deltaTime;
// Apply movement to the Rigidbody
rb.transform.position += movement;
// Calculate mouse X movement change
float mouseXRaw = Input.mousePosition.x - 1094 / 2;
float mouseMovedX = mouseXRaw - previousX;
previousX = mouseXRaw;
float mouseX = mouseMovedX * sensitivity;
rb.transform.Rotate(Vector3.up * mouseMovedX);
// Calculate mouse Y movement change
float mouseYRaw = Input.mousePosition.y - 1094 / 2;
float mouseMovedY = mouseYRaw - previousY;
previousY = mouseYRaw;
float mouseY = mouseMovedY * sensitivity;
camera.transform.Rotate(Vector3.left * mouseMovedY);
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
}
}