-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
70 lines (57 loc) · 1.66 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
69
70
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
PlayerInput playerInput;
InputAction moveAction;
Rigidbody rb;
public static PlayerController instance;
public float moveSpeed = 100f;
public float jumpForce = 10f;
private bool isGrounded = true;
private float fixedZPosition;
public int player;
void Start()
{
instance = this;
playerInput = GetComponent<PlayerInput>();
if (player == 1)
{
moveAction = playerInput.actions["P1Move"];
}
else
{
moveAction = playerInput.actions["P2Move"];
}
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotation;
fixedZPosition = transform.position.z;
}
void FixedUpdate()
{
MovePlayer();
}
void MovePlayer()
{
Vector2 direction = moveAction.ReadValue<Vector2>();
Vector3 move = new Vector3(direction.x, 0f, 0f) * moveSpeed * Time.deltaTime;
Vector3 newPosition = rb.position + move;
// Clamp the x position within the specified range
newPosition.x = Mathf.Clamp(newPosition.x, -8.71f, 8.60f);
// Maintain the fixed z position
newPosition.z = fixedZPosition;
rb.MovePosition(newPosition);
if (isGrounded && direction.y > 0)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
}