Created
January 29, 2018 19:32
-
-
Save tklee1975/4896dbec0f25080adec7668d40065a2a to your computer and use it in GitHub Desktop.
Move Script for 2d Object in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| namespace SimpleTDD { | |
| public class TDDController2D : MonoBehaviour { | |
| public enum MoveType { | |
| SimpleTranslate, | |
| RaycastCheck, | |
| }; | |
| public float speed = 5.0f; | |
| public MoveType moveType = MoveType.RaycastCheck; | |
| public bool cameraFollow = false; | |
| // Use this for initialization | |
| protected Rigidbody2D rigidBody; | |
| protected ContactFilter2D contactFilter; | |
| protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16]; | |
| void Start() { | |
| // | |
| rigidBody = GetComponent<Rigidbody2D>(); | |
| // Define the contact Filter, need when do RayCast | |
| contactFilter.useTriggers = false; | |
| contactFilter.SetLayerMask (Physics2D.GetLayerCollisionMask (gameObject.layer)); | |
| contactFilter.useLayerMask = true; | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| HandleMovement (); | |
| } | |
| void MoveUsingTranslate(Vector2Int moveVec) | |
| { | |
| Vector3 transVec = new Vector3(moveVec.x, moveVec.y, 0f) * speed * Time.deltaTime; | |
| transform.Translate (transVec); | |
| } | |
| void MoveUsingRayCast(Vector2Int moveVec) | |
| { | |
| Vector2 move = new Vector2 (moveVec.x, moveVec.y) * speed * Time.deltaTime; | |
| float distance = move.magnitude; | |
| int count = rigidBody.Cast (move, contactFilter, hitBuffer, distance); | |
| for (int i = 0; i < count; i++) { | |
| RaycastHit2D hit = hitBuffer [i]; | |
| float modifiedDistance = hit.distance; | |
| distance = modifiedDistance < distance ? modifiedDistance : distance; | |
| } | |
| rigidBody.position = rigidBody.position + move.normalized * distance; | |
| } | |
| void Move(Vector2Int moveVec) { | |
| if (MoveType.RaycastCheck == moveType) { | |
| MoveUsingRayCast (moveVec); | |
| } else { | |
| MoveUsingTranslate (moveVec); | |
| } | |
| } | |
| void UpdateCameraPos() { | |
| float x = transform.position.x; | |
| float y = transform.position.y; | |
| Vector3 pos = Camera.main.transform.position; | |
| pos.x = x; | |
| pos.y = y; | |
| Camera.main.transform.position = pos; | |
| } | |
| void HandleMovement() { | |
| Vector2Int moveVec = Vector2Int.zero; | |
| if (Input.GetKey (KeyCode.UpArrow)) { | |
| moveVec = new Vector2Int (0, 1); | |
| } else if (Input.GetKey (KeyCode.DownArrow)) { | |
| moveVec = new Vector2Int(0, -1); | |
| } else if (Input.GetKey (KeyCode.LeftArrow)) { | |
| moveVec = new Vector2Int(-1, 0); | |
| } else if (Input.GetKey (KeyCode.RightArrow)) { | |
| moveVec = new Vector2Int(1, 0); | |
| } | |
| Move (moveVec); | |
| if (cameraFollow) { | |
| UpdateCameraPos (); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment