일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Two Points
- MYSQL
- DP
- Brute Force
- binary search
- Stored Procedure
- 스토어드 프로시저
- 다익스트라
- Dijkstra
- 그래프
- Trie
- 이진탐색
- union find
- two pointer
- String
- SQL
- Hash
Archives
- Today
- Total
codingfarm
오브젝트 이동하기 본문
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteTest2 : MonoBehaviour{
// Start is called before the first frame update
enum state {idle, walk}
public Sprite[] idle;
public Sprite[] walk;
public SpriteRenderer spriteRenderer;
public float anim_time;
int animation_index;
state state_;
void Start() {
spriteRenderer = transform.GetChild(0).GetComponent<SpriteRenderer>();
animation_index = 0;
state_ = 0;
anim_time = 0;
}
// Update is called once per frame
void Update() {
anim_time += Time.deltaTime;
if (anim_time > 0.12f) {
anim_time = 0;
if (state_ == state.walk) {
if (animation_index >= walk.Length)
animation_index = 0;
spriteRenderer.sprite = walk[animation_index];
}
if (state_ == state.idle) {
if (animation_index >= idle.Length)
animation_index = 0;
spriteRenderer.sprite = idle[animation_index];
}
animation_index++;
}
Vector3 movingVector;
movingVector = Vector3.zero;
if (Input.GetKey(KeyCode.RightArrow)) {
//Debug.Log("R");
movingVector.x += 1f;
}
if (Input.GetKey(KeyCode.LeftArrow)) {
//Debug.Log("L");
movingVector.x -= 1f;
}
if (Input.GetKey(KeyCode.UpArrow)) {
//Debug.Log("U");
movingVector.z += 1f;
}
if (Input.GetKey(KeyCode.DownArrow)) {
//Debug.Log("D");
movingVector.z -= 1f;
}
if(movingVector == Vector3.zero) {
if(state_ != state.idle) {
animation_index = 0;
state_ = state.idle;
anim_time = 10f;
}
}
else {
if (state_ != state.walk) {
animation_index = 0;
state_ = state.walk;
anim_time = 10f;
}
}
movingVector.Normalize();
movingVector *= (Time.deltaTime * 2);
movingVector.z *= 1.7f;
//movingVector.x = movingVector.x * Time.deltaTime * 0.01f;
//movingVector.z = movingVector.z * Time.deltaTime * 0.01f;
movingVector = transform.TransformVector(movingVector);
transform.Translate(movingVector);
}
}
|
cs |
스위치문으로 상태를 정의하는것 보다는 fsm을 사용하는게 간편하다.
물리엔진을 이용하고 싶다면 rigidbody를 쓰면 된다.
이렇게 하면 collider와의 충돌 판정이 보다 용이해진다.
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 95 96 97 98 99 100 101 102 103 104 105 106 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpriteTest2 : MonoBehaviour{ // Start is called before the first frame update enum state {idle, walk}; enum dir {left, right}; public Sprite[] idle; public Sprite[] walk; public SpriteRenderer spriteRenderer; public Transform groundChecker; Transform spriteObject; Rigidbody myRigidbody; float y_speed; public float anim_time; int animation_index; state state_; dir dir_; void Start() { spriteObject = transform.GetChild(0); spriteRenderer = spriteObject.GetComponent<SpriteRenderer>(); myRigidbody = GetComponent<Rigidbody>(); animation_index = 0; state_ = 0; anim_time = 0; } // Update is called once per frame void Update() { anim_time += Time.deltaTime; if (anim_time > 0.12f) { anim_time = 0; if (state_ == state.walk) { if (animation_index >= walk.Length) animation_index = 0; spriteRenderer.sprite = walk[animation_index]; } if (state_ == state.idle) { if (animation_index >= idle.Length) animation_index = 0; spriteRenderer.sprite = idle[animation_index]; } animation_index++; } Vector3 movingVector; movingVector = Vector3.zero; if (Input.GetKey(KeyCode.RightArrow)) { //Debug.Log("R"); movingVector.x += 1f; if (dir_ != dir.right) { dir_ = dir.right; spriteObject.transform.localScale = new Vector3(1, 1, 1); } } if (Input.GetKey(KeyCode.LeftArrow)) { //Debug.Log("L"); movingVector.x -= 1f; if (dir_ != dir.left) { dir_ = dir.left; spriteObject.transform.localScale = new Vector3(-1, 1, 1); } } if (Input.GetKey(KeyCode.UpArrow)) { //Debug.Log("U"); movingVector.z += 1f; } if (Input.GetKey(KeyCode.DownArrow)) { //Debug.Log("D"); movingVector.z -= 1f; } if(movingVector == Vector3.zero) { if(state_ != state.idle) { animation_index = 0; state_ = state.idle; anim_time = 10f; } } else { if (state_ != state.walk) { animation_index = 0; state_ = state.walk; anim_time = 10f; } } movingVector.Normalize(); movingVector *= (Time.deltaTime * 2); movingVector.z *= 1.7f; //movingVector.x = movingVector.x * Time.deltaTime * 0.01f; //movingVector.z = movingVector.z * Time.deltaTime * 0.01f; movingVector = transform.TransformVector(movingVector); //transform.Translate(movingVector); myRigidbody.MovePosition(myRigidbody.position + movingVector); } } | cs |
'Unity5 > 기타' 카테고리의 다른 글
3. 2d 플랫포머 - 캐릭터 이동 (0) | 2020.07.26 |
---|---|
코루틴 (0) | 2020.06.06 |
mechanim(2d) (0) | 2020.06.05 |
Ray와 Raycast충돌 (0) | 2020.06.05 |
sprite 변경하기 (0) | 2020.06.05 |
Comments