-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerJump.cs
71 lines (57 loc) · 1.72 KB
/
PlayerJump.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
[Header("Jump Details")]
public float jumpforce;
public float jumpTime;
private float jumpTimeCounter;
private bool stoppedJumping;
[Header("Ground Details")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float radOCircle;
[SerializeField] private LayerMask whatIsGround;
public bool grounded;
[Header("Components")]
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
jumpTimeCounter = jumpTime;
}
private void Update()
{
//what it means to be grounded
grounded = Physics2D.OverlapCircle(groundCheck.position,radOCircle,whatIsGround);
if (grounded)
{
jumpTimeCounter = jumpTime;
}
//use space and joystick button 3 to jump
//if we press the jump button
if (Input.GetButtonDown("Jump") && grounded)
{
//jump!!!
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
stoppedJumping = false;
}
//keeping jumping!!! (while button is active)
if (Input.GetButton("Jump") && !stoppedJumping && (jumpTimeCounter > 0))
{
//jump!!!
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
jumpTimeCounter -= Time.deltaTime;
}
//if we release the button
if (Input.GetButtonUp("Jump"))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
}
private void OnDrawGizmos()
{
Gizmos.DrawSphere(groundCheck.position, radOCircle);
}
}