78 lines
2.0 KiB
C#
78 lines
2.0 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class TouchController : BaseController
|
||
{
|
||
/// <summary>
|
||
/// 控制对象的Transform
|
||
/// 若为空则控制当前脚本所在对象
|
||
/// </summary>
|
||
[Tooltip("控制对象的Transform,默认为当前对象")]
|
||
public Transform tran;
|
||
/// <summary>
|
||
/// 移动速度(米/秒)
|
||
/// </summary>
|
||
[Tooltip("移动速度(米/秒)")]
|
||
public float moveSpeed = 5;
|
||
/// <summary>
|
||
/// 旋转速度(角度/秒)
|
||
/// </summary>
|
||
[Tooltip("旋转速度(角度/秒)")]
|
||
public float rotateSpeed = 10;
|
||
|
||
/// <summary>
|
||
/// 点击位置
|
||
/// </summary>
|
||
Vector2 startTouchPosition;
|
||
void Start()
|
||
{
|
||
if (tran == null)
|
||
tran = transform;
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (CanControl)
|
||
{
|
||
if (Input.touchCount > 0)
|
||
{
|
||
Touch touch = Input.GetTouch(0);
|
||
|
||
if (touch.phase == TouchPhase.Began)
|
||
{
|
||
startTouchPosition = touch.position;
|
||
}
|
||
else if (touch.phase == TouchPhase.Moved)
|
||
{
|
||
Vector2 deltaTouch = touch.position - startTouchPosition;
|
||
|
||
tran.position += tran.forward * moveSpeed * Time.deltaTime * deltaTouch.y; // 移动
|
||
|
||
if (deltaTouch.x > 0)
|
||
tran.Rotate(Vector3.up * rotateSpeed * Time.deltaTime ); // 旋转
|
||
else
|
||
tran.Rotate(Vector3.up * -rotateSpeed * Time.deltaTime); // 旋转
|
||
|
||
IsTouchOnControl = true;
|
||
}
|
||
else
|
||
{
|
||
IsTouchOnControl = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if(IsTouchOnControl)
|
||
{
|
||
Invoke("SetTouch", 1f);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void SetTouch()
|
||
{
|
||
IsTouchOnControl = false;
|
||
}
|
||
} |