Files
TaiWan/Assets/Roaming/Scripts/Controller/Camera/TouchController.cs
2025-10-31 15:20:38 +08:00

78 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}