Skip to content

Instantly share code, notes, and snippets.

@ezaurum
Created April 12, 2015 11:03
Show Gist options
  • Save ezaurum/f3c096dc0b73e53ceae8 to your computer and use it in GitHub Desktop.
Save ezaurum/f3c096dc0b73e53ceae8 to your computer and use it in GitHub Desktop.
UnityTouchAndMouseMove
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