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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | using UnityEngine; using System.Collections;
public class MoveTest : MonoBehaviour {
public float speed = 1.0F; public float grid = 1.0f; public float rotationSpeed = 100.0F;
[SerializeField] Direction direction = Direction.NONE;
Vector3 startPosition; Vector3 endPosition = Vector3.zero; Vector3 dir = Vector3.zero;
Hashtable hash = new Hashtable();
bool isMoving = false;
enum Direction { LEFT, RIGHT, FORWARD, BACK, NONE };
Animator animator; void Start() { animator = GetComponent<Animator>(); }
// Update is called once per frame
void Update() { if (Input.GetKeyDown(KeyCode.DownArrow)) direction = Direction.BACK; else if (Input.GetKeyDown(KeyCode.LeftArrow)) direction = Direction.LEFT; else if (Input.GetKeyDown(KeyCode.RightArrow)) direction = Direction.RIGHT; else if (Input.GetKeyDown(KeyCode.UpArrow)) direction = Direction.FORWARD;
if(isMoving == false) Move(); }
void Move() { isMoving = true; startPosition = transform.position;
switch(direction) { case Direction.LEFT: hash.Clear(); endPosition = startPosition + transform.right * grid * -1;
dir = endPosition - startPosition; dir.y = 0.0f; dir.Normalize();
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), rotationSpeed);
hash.Clear(); hash.Add("position", endPosition); hash.Add("speed", speed); hash.Add("easetype", iTween.EaseType.linear); iTween.MoveTo(gameObject, hash);
animator.SetTrigger("Move");
break; case Direction.RIGHT:
hash.Clear(); endPosition = startPosition + transform.right * grid;
dir = endPosition - startPosition; dir.y = 0.0f; dir.Normalize(); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), rotationSpeed);
hash.Add("position", endPosition); hash.Add("speed", speed); hash.Add("easetype", iTween.EaseType.linear); iTween.MoveTo(gameObject, hash);
animator.SetTrigger("Move"); break; case Direction.FORWARD:
hash.Clear();
endPosition = startPosition + transform.forward * grid;
hash.Add("position", endPosition); hash.Add("speed", speed); hash.Add("easetype", iTween.EaseType.linear); iTween.MoveTo(gameObject, hash);
animator.SetTrigger("Move"); break;
case Direction.BACK:
hash.Clear(); endPosition = startPosition + transform.forward * grid * -1;
dir = endPosition - startPosition; dir.y = 0.0f; dir.Normalize(); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), rotationSpeed);
hash.Add("position", endPosition); hash.Add("speed", speed); hash.Add("easetype", iTween.EaseType.linear); iTween.MoveTo(gameObject, hash);
animator.SetTrigger("Move"); break;
}
transform.position = new Vector3(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y), Mathf.Round(transform.position.z)); transform.rotation = new Quaternion(Mathf.Round(transform.rotation.x), Mathf.Round(transform.rotation.y), Mathf.Round(transform.rotation.z), Mathf.Round(transform.rotation.w));
direction = Direction.NONE; isMoving = false; } }
|
Grid Move 코드
itween으로 부드럽게 움직이게 했고 움직임에 따라 회전하게 만듬
또 움직이다보면 정확히 안움직이는데 다 반올림시켜서 움직이게 만듬
grid단위가 1이여서 편했네
'Unity' 카테고리의 다른 글
NGUI 공식 Tutorial 정리 (0) | 2015.07.03 |
---|---|
animator에 붙어있는 animation의 재생여부 (0) | 2015.06.11 |
Rigidbody 초기화 (0) | 2015.05.15 |
Humanoid 머리에 콜라이더 달기 (0) | 2015.05.14 |
Animator에서 Trigger 와 Bool의 차이 (0) | 2015.05.14 |