This repository has been archived by the owner on Oct 5, 2020. It is now read-only.
/
HeroController.cs
72 lines (60 loc) · 1.56 KB
/
HeroController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroController : Player {
public GameObject playerManager;
public float walkSpeed = 2.0f;
private string state = "alive";
private AnimationScript animationScript;
void Awake()
{
animationScript = GetComponent<AnimationScript>();
}
void OnCollisionEnter2D(Collision2D collision) {
if (!enabled) {
return;
}
Debug.Log(collision.gameObject.name);
if (collision.gameObject.tag == "deadlyOnTouch") {
playerManager.SendMessage("Die");
Die();
return;
}
}
void FixedUpdate() {
float horizontalVelocity = Input.GetAxis("Horizontal");
Move(horizontalVelocity);
}
public void Move(float amount) {
if(state != "alive") return;
/*if (amount == 0) {
walkSpeed = 2;
}*/
var walkAmount = amount * walkSpeed * Time.fixedDeltaTime;
transform.Translate(walkAmount * Vector2.right);
if(walkAmount < 0)
{
animationScript.ChangeAnim("Hero/HeroWalk");
animationScript.frameTime = 0.2f;
animationScript.flipX = true;
}
else
if(walkAmount > 0)
{
animationScript.ChangeAnim("Hero/HeroWalk");
animationScript.frameTime = 0.2f;
animationScript.flipX = false;
}
else
{
animationScript.ChangeAnim("Hero/HeroIdle");
animationScript.frameTime = 0.4f;
}
}
public void Die()
{
animationScript.ChangeAnim("Hero/HeroDeath", false);
animationScript.frameTime = 0.13f;
state = "dead";
}
}