Created
April 12, 2015 11:03
-
-
Save ezaurum/f3c096dc0b73e53ceae8 to your computer and use it in GitHub Desktop.
UnityTouchAndMouseMove
This file contains 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 UnityEngine; | |
namespace PhotonTest | |
{ | |
public class CharacterController : MonoBehaviour | |
{ | |
private Vector3 _destination; | |
private RaycastHit _raycastHit; | |
private Vector3 _touch; | |
public float MoveSpeed; | |
private bool _moving; | |
// Use this for initialization | |
private void Start() | |
{ | |
_moving = false; | |
} | |
// Update is called once per frame | |
private void Update() | |
{ | |
#if UNITY_EDITOR || UNITY_STANDALONE | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
_moving = true; | |
} | |
else if (Input.GetMouseButtonUp(0)) | |
{ | |
_moving = false; | |
} | |
_touch = Input.mousePosition; | |
#elif UNITY_ANDROID || UNITY_IOS | |
if (Input.touchCount > 0) | |
{ | |
var pos = Input.GetTouch(0).position; | |
_touch = new Vector3(pos.x, pos.y, 0.0f); | |
_moving = Input.GetTouch(0).phase != TouchPhase.Ended; | |
} | |
#endif | |
if (!_moving) return; | |
if (!Physics.Raycast(Camera.main.ScreenPointToRay(_touch), out _raycastHit, Mathf.Infinity)) return; | |
_destination = _raycastHit.point; | |
transform.Translate((_destination - transform.position).normalized * MoveSpeed * Time.deltaTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment