-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovingPlayerButtonTouchScreen.cs
89 lines (81 loc) · 2.05 KB
/
MovingPlayerButtonTouchScreen.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
83
84
85
86
87
88
89
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlayer : MonoBehaviour
{
Rigidbody2D rb2D;
public Animator heroAnim;
public float Velocity, Speed, jumpForce;
bool canJump;
// Start is called before the first frame update
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
//Animação do hero estar parada (idle)
heroAnim.SetBool("Jump", false);
heroAnim.SetBool("Run", false);
heroAnim.SetBool("Walk", false);
}
// Update is called once per frame
void Update()
{
Move(Speed);
}
void Move(float _speed)
{
rb2D.transform.Translate(_speed * Time.deltaTime, 0, 0);
heroAnim.SetBool("Walk", true);
Flip();
}
private void Flip()
{
if(Speed < 0)
{
if(gameObject.GetComponent<SpriteRenderer>().flipX == false)
{
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
}
if (Speed > 0)
{
if (gameObject.GetComponent<SpriteRenderer>().flipX == true)
{
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
}
}
public void MoveRight()
{
Speed = Velocity;
}
public void MoveLeft()
{
Speed = -Velocity;
}
public void StopMove()
{
Speed = 0;
}
public void Jump()
{
if (canJump)
{
rb2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
heroAnim.SetBool("Jump", true);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("ground"))
{
canJump = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("ground"))
{
canJump = false;
}
}
}