-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
65 lines (57 loc) · 1.72 KB
/
Copy pathPlayerController.cs
File metadata and controls
65 lines (57 loc) · 1.72 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D player;
private PlayerInputControls playerInputControls;
private StatsHandler playerStats;
private bool runToggle = false;
public Vector2 mousePosition;
private void Awake(){
player = GetComponent<Rigidbody2D>();
playerStats = GetComponent<StatsHandler>();
playerInputControls = new PlayerInputControls();
playerInputControls.Player.Enable();
}
public void Update()
{
MousePositionReporter();
}
public void FixedUpdate(){
DirectionalMovementHandler();
}
public void DirectionalMovementHandler()
{
Vector2 inputVector = playerInputControls.Player.Movement.ReadValue<Vector2>() * playerStats.currentSpeed;
if (inputVector != new Vector2(0f, 0f))
{
player.velocity = inputVector;
if (Mathf.Abs(inputVector.x) > 0.5f && Mathf.Abs(inputVector.y) > 0.5f)
{
player.velocity = new Vector2(inputVector.x, inputVector.y / 2);
}
}
else if (inputVector == new Vector2(0f, 0f))
{
player.Sleep();
}
}
public void RunToggler()
{
runToggle = !runToggle;
if (runToggle == true)
{
playerStats.currentSpeed *= 2f;
}
else
{
playerStats.currentSpeed = playerStats.moveSpeed;
}
}
public void MousePositionReporter()
{
mousePosition = Mouse.current.position.ReadValue();
}
}