-
Notifications
You must be signed in to change notification settings - Fork 5
/
Movement_2D_Platformer.cs
51 lines (44 loc) · 1.38 KB
/
Movement_2D_Platformer.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Movement_2D_Platformer : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float _NormalSpeed = 5;
[SerializeField] private float _SprintSpeed = 8;
[SerializeField] private float _JumpSpeed = 300;
[SerializeField] private float _GroundCheck = 0.6f;
[Header("Set ground layer")]
[SerializeField] private LayerMask _GroundMask = ~1;
private float _Speed = 0;
private Rigidbody2D _RB;
void Start()
{
//Get Rigidbody / Lock z rotation
_RB = GetComponent<Rigidbody2D>();
_RB.constraints = RigidbodyConstraints2D.FreezeRotation;
}
void Update()
{
//Sprint
if (Input.GetKey(KeyCode.LeftShift))
_Speed = _SprintSpeed;
else
_Speed = _NormalSpeed;
//Jumping
if (Input.GetButtonDown("Jump") && IsGrounded())
_RB.AddForce(new Vector2(0, _JumpSpeed));
//Apply Movement
_RB.velocity = new Vector2(Input.GetAxis("Horizontal") * _Speed, _RB.velocity.y);
}
bool IsGrounded()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, _GroundCheck, _GroundMask);
if (hit.collider != null)
{
return true;
}
return false;
}
}