-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehaviour.cs
79 lines (67 loc) · 2.02 KB
/
Behaviour.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Behaviour : MonoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public int extraJumps;
public int extraJumpsValue;
//for transition
public Animator animator;
// Start is called before the first frame update
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput < 0)
{
Flip();
}
}
void Update()
{
animator.SetFloat("Speed", Mathf.Abs(moveInput * speed));
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
animator.SetBool("IsJumping", false);
}
if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
animator.SetBool("IsJumping", true);
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
animator.SetBool("IsJumping", true);
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}