-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
96 lines (80 loc) · 3.38 KB
/
Player.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
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private bool _isFacingRight;
// Will keep track of our CharacterController2D as this player
// instance moves through the world.
private CharacterController2D _controller;
// Will either be 1 or -1 (hence normalized)
// depending on whether the player is moving right or left.
private float _normalizedHorizontalSpeed;
// The following 3 values
public float MaxSpeed = 8; //maximum units per second that the player can move
// These two determine how quickly a player can go from
// not moving at all to moving (or from moving left to moving right)
// in other words, how much inertia the player experiences
// depending on the medium it finds itself in.
public float SpeedAccelerationOnGround = 10f;
public float SpeedAccelerationInAir = 5f;
public void Start()
{
// instantiate our CharacterCOntroller2D components
_controller = GetComponent<CharacterController2D>();
// We are facing right if our character is flipped
// So this initializes our _isFacingRight var.
_isFacingRight = transform.localScale.x > 0;
}
public void Update()
{
// This will change our normalized Horizontal Speed
// to 1, -1 or 0 depending on whether the player is holding down
// A or D.
HandleInput();
// Will return the correct acceleration factor depending on whether th eplayer is on the ground or in the air.
// State is instanciated in the CharacterCOntroller2D.
var movementFactor = _controller.State.IsGrounded ? SpeedAccelerationOnGround : SpeedAccelerationInAir;
// Again, the arguments for Mathf.lerp are current-velocity, maximal possible speed * instantaneous direction of movememnt, and time
_controller.SetHorizontalForce(Mathf.Lerp(_controller.Velocity.x, _normalizedHorizontalSpeed * MaxSpeed, Time.deltaTime*movementFactor));
}
private void HandleInput()
{
//User is pressing D - which is the 'go right' command
if (Input.GetKey(KeyCode.D))
{
// We go right
_normalizedHorizontalSpeed = 1;
// If the player is not facing right, do a flip
if(!_isFacingRight)
{
Flip();
}
}
else if (Input.GetKey(KeyCode.A))
{
_normalizedHorizontalSpeed = -1;
if(_isFacingRight)
{
Flip();
}
}
else
{
_normalizedHorizontalSpeed = 0;
}
// If the player can jump, then the space bar will make it happen.
if (_controller.CanJump && Input.GetKeyDown(KeyCode.Space))
{
_controller.Jump();
}
}
private void Flip()
{
// Negate the x-component of the player's localscale, flipping the character around.
// Remember that the localScale is the 3rd parameter down in the transform pane
// in the Inspector. Flipping the PLayer backwards makes the x-coordinate go
// negative in live mode.
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
// Will set or reset _isFacingRight by testing localscale.x against 0 as a boolean.
_isFacingRight = transform.localScale.x > 0;
}
}