unity中触屏控制人物移动

         例如在飞机大战中,让战机随手指移动而移动

public class Hero : MonoBehaviour {

    private bool isMouseDown = false;
    private Vector3 lastMousePosition = Vector3.zero;

	void Update () {
		if(Input .GetMouseButtonDown(0))
        {
            isMouseDown = true;
        }
        if(Input .GetMouseButtonUp (0))
        {
            isMouseDown = false;
            lastMousePosition = Vector3.zero;
        }
        if(isMouseDown)
        {
            if(lastMousePosition !=Vector3 .zero)
            {
                Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
                transform.position += offset;
            }
            lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
	}
}

 

你可能感兴趣的:(Unity3D引擎)