-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
106 lines (87 loc) · 3.48 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Movement mechanics variables
public float maxSpeed = 3.4f;
public float jumpHeight = 6.5f;
public float gravityScale = 1.5f;
// Movement state machine: 0 is still, -1 is left, 1 is right
float moveDirection = 0;
bool isGrounded = false;
// Object component references
Rigidbody2D r2d;
CapsuleCollider2D mainCollider;
// To get camera to follow Player:
// 1. Add/install Cinemachine from Unity package manager
// 2. Add a Cinemachine 2D Camera object to scene
// 3. Drag and drop Player object into the 2D Camera's 'follow' field
// A Cinemachine virtual camera is like a cameraman controlling the position and settings
// of the Main Camera, but not actually a camera itself.
// Use this for initialization
void Start()
{
// initialize object component variables
r2d = GetComponent<Rigidbody2D>();
mainCollider = GetComponent<CapsuleCollider2D>();
// If freezeRotation is enabled, the rotation in Z is not modified by the physics simulation.
// Good for 2D!
r2d.freezeRotation = true;
// Ensures that all collisions are detected when a Rigidbody2D moves.
r2d.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
// Apply gravity scale to Player's Rigidbody2D
r2d.gravityScale = gravityScale;
}
// Update is called once per frame (e.g. runs 24 times at 24 frames per second).
// Use FixedUpdate if you require consistent time between method calls.
void Update()
{
// Movement controls (left and right)
if (Input.GetKey(KeyCode.A))
{
moveDirection = -1;
}
else if (Input.GetKey(KeyCode.D))
{
moveDirection = 1;
}
else if (isGrounded || r2d.velocity.magnitude < 0.01f)
{
moveDirection = 0;
}
// Jumping
if (Input.GetKeyDown(KeyCode.W) && isGrounded)
{
// Apply movement velocity in the y direction
r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight);
}
}
// Called at fixed intervals regardless of frame rate, unlike the Update method.
void FixedUpdate()
{
// Get information from Player's collider
Bounds colliderBounds = mainCollider.bounds;
float colliderRadius = mainCollider.size.x * 0.4f * Mathf.Abs(transform.localScale.x);
// Position to check for if grounded
Vector3 groundCheckPos = colliderBounds.min + new Vector3(colliderBounds.size.x * 0.5f, colliderRadius * 0.9f, 0);
//Access all overlapping colliders at groundCheckPos
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckPos, colliderRadius);
isGrounded = false;
//Check if any of the overlapping colliders are not player collider, if so, set isGrounded to true
if (colliders.Length > 0)
{
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i] != mainCollider)
{
isGrounded = true;
Debug.Log("Landed on: " + colliders[i]);
break;
}
}
}
// Apply movement velocity in the x direction
r2d.velocity = new Vector2((moveDirection) * maxSpeed, r2d.velocity.y);
}
}