Unity关于触摸屏相机视角的控制方式(鼠标也可操作)

  1. 触屏软件功能展示对于相机视角的控制使用
using UnityEngine;
using UnityEngine.EventSystems;

public class CameraController : MonoBehaviour
{
   
    public GameObject Target;
    public float LastY;
    public float Distance = 10f;
    public float MinDistance = 5f;
    public float MaxDistance = 15f;
    public float Sensitivity = 5f;
    public float MoveSensitivity = 3;
    public float rotationX = 0f;
    public float rotationY = 0f;
    public Vector3 offset;
    public Quaternion targetRotation;
    public Vector3 targetPosition;
    [Header("鼠标位移")]
    public bool IsDragging;
    private Vector3 lastPosition;
    [Header("点击跟随")]
    public bool IsMovingByObj = true;
    [Header("手势")]

    //旧的触摸点位置
    Vector2 oldPos1;
    Vector2 oldPos2;

    public float speed = 0.7f;

    public float RotaTouchHor = 0.7f;
   
    public BoxCollider moveBox;

    public BoxCollider TargetBox;

    public bool IsCanRota = true;

 

    private void Start()
    {
   
        offset = transform.position - Target.transform.position;
        //speed =4;
        //RotaTouchHor =4;
        TargetObjPos = Target.transform.position;
        
    }

 

 

    private void Update()
    {
   

        if (Input.touchCount < 2)
        {
   
            if (Input.touchCount > 0)
            {
   
                Touch touch = Input.GetTouch(0);
                if (touch.phase == TouchPhase.Moved)
                {
   
                    float touchDeltaPositionX = touch.deltaPosition.x;
                    float touchDeltaPositionY = touch.deltaPosition.y;
                    // Do something with the touchDeltaPositionX value, such as using it to rotate an object
                    rotationX += touchDeltaPositionX * speed;
                    rotationY -= touchDeltaPositionY * speed;
                    rotationY = Mathf.Clamp(rotationY, -90f, 89f);
                    targetRotation = Quaternion.Euler(rotationY, rotationX, 0f);
                }
            }
            if (Input.GetMouseButton(1) && Input.touchCount == 0 && !EventSystem.current.IsPointerOverGameObject())
            {
   

                IsDragging = true;
                lastPosition = Input.mousePosition
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰尼杰尼丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值