-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerMovement.cs
67 lines (57 loc) · 2.69 KB
/
PlayerMovement.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
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerMovement : MonoBehaviour {
public int speed;
private bool isMoving;
private Vector2 lastMove;
private Animator animat;
private Rigidbody2D myrb;
public static bool CanMove;
public static PlayerMovement Player;
public bool StartD;
void Awake()
{
if (Player == null)
{
DontDestroyOnLoad(gameObject);
Player = this;
}
else if (Player != this)
{
Destroy(gameObject);
}
}
void Start () {
animat = GetComponent<Animator>(); //Vzimam animaciqta
myrb = GetComponent<Rigidbody2D>(); // Vzimam rigidbodyto na player
}
void Update () {
isMoving = false;
if ((CrossPlatformInputManager.GetAxis("Horizontal") > 0.1f || CrossPlatformInputManager.GetAxis("Horizontal") < -0.1f) && CanMove) // Dvijenie na lqvo/dqsno
{
myrb.velocity = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal") * speed * Time.deltaTime, myrb.velocity.y);
isMoving = true;
lastMove = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), 0f);
}
if ((CrossPlatformInputManager.GetAxis("Vertical") > 0.1f || CrossPlatformInputManager.GetAxis("Vertical") < -0.1f) && CanMove) //Dvijenie gore/dolu
{
myrb.velocity = new Vector2(myrb.velocity.x, CrossPlatformInputManager.GetAxis("Vertical") * speed * Time.deltaTime);
isMoving = true;
lastMove = new Vector2(0f, CrossPlatformInputManager.GetAxis("Vertical"));
}
if ((CrossPlatformInputManager.GetAxis("Horizontal") == 0f && CrossPlatformInputManager.GetAxis("Horizontal") == 0f) || !CanMove) //Spirane na horizontalnata inerciqta, porodena ot dvijenieto na chovecheto
{
myrb.velocity = new Vector2(0f, myrb.velocity.y);
}
if ((CrossPlatformInputManager.GetAxis("Vertical") == 0f && CrossPlatformInputManager.GetAxis("Vertical") == 0f) || !CanMove) //Spirane na verticalnata inerciqta, porodena ot dvijenieto na chovecheto
{
myrb.velocity = new Vector2(myrb.velocity.x, 0f);
}
animat.SetFloat("Horizontal", CrossPlatformInputManager.GetAxisRaw("Horizontal")); //Zadavam stoinosta na promenlivata "Horizontal" na animaciqta
animat.SetFloat("Vertical", CrossPlatformInputManager.GetAxisRaw("Vertical")); //Zadavam stoinosta na promenlivata "Vertical" na animaciqta
animat.SetFloat("LastHorizontal", lastMove.x);
animat.SetFloat("LastVertical", lastMove.y);
animat.SetBool("IsMoving", isMoving);
}
}