-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerController.cs
82 lines (72 loc) · 1.96 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
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine.SceneManagement;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public float JumpForce;
private float MoveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public int health;
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
MoveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(MoveInput * speed, rb.velocity.y);
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (MoveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if (MoveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (MoveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("TakeOff");
rb.velocity = Vector2.up * JumpForce;
}
if (isGrounded == false)
{
anim.SetBool("isJumping", true);
}
if (health <= 0)
{
Destroy(gameObject);
restart();
}
}
public void TakeDamage(int damage)
{
health -= damage;
}
void restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}