-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerAction.cs
94 lines (83 loc) · 2.46 KB
/
PlayerAction.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
90
91
92
93
94
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAction : MonoBehaviour
{
// Start is called before the first frame update
float h;
float v;
public float speed;
bool isHorizonMove;
Animator anim;
Vector3 dirVec;
Rigidbody2D rigid;
GameObject scanObject;
void Awake()
{
rigid =GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
h = Input.GetAxisRaw("Horizontal");
v = Input.GetAxisRaw("Vertical");
bool hDown = Input.GetButtonDown("Horizontal");
bool vDown = Input.GetButtonDown("Vertical");
bool hUp = Input.GetButtonUp("Horizontal");
bool vUp = Input.GetButtonUp("Vertical");
if (hDown)
isHorizonMove = true;
else if (vDown)
isHorizonMove = false;
else if (hUp || vUp)
isHorizonMove = h != 0;
//Animation
if (anim.GetInteger("hAxisRaw") != h)
{
anim.SetBool("isChange", true);
anim.SetInteger("hAxisRaw", (int)h);
}
else if(anim.GetInteger("vAxisRaw") != v)
{
anim.SetBool("isChange", true);
anim.SetInteger("vAxisRaw", (int)v);
}
else
{
anim.SetBool("isChange",false);
}
//Direction
if(vDown && v == 1)
{
dirVec = Vector3.up;
}else if(vDown && v == -1)
{
dirVec = Vector3.down;
}else if (hDown && h == -1)
{
dirVec = Vector3.left;
}else if(hDown && h == 1)
{
dirVec = Vector3.right;
}
//scan object
if (Input.GetButtonDown("Jump") && scanObject != null)
{
Debug.Log("this is : "+scanObject.name);
}
}
void FixedUpdate()
{
Vector2 moveVec = isHorizonMove ? new Vector2(h, 0) : new Vector2(0, v);
rigid.velocity = moveVec*speed;
//Ray
Debug.DrawRay(rigid.position, dirVec*0.7f, new Color(0,1,0));
RaycastHit2D rayhit = Physics2D.Raycast(rigid.position, dirVec, 0.7f, LayerMask.GetMask("Object"));
if(rayhit.collider != null)
{
scanObject = rayhit.collider.gameObject;
}else
scanObject = null;
}
}