个人代码整理(相机移动推拉)

/***********************

  • Title: “移动端控制摄像机”

  • Func: 1、平移摄像机

  •      2、推进、拉远摄像机(近大远小,效果:模型缩放)
    
    •    3、旋转摄像机
      
  • UsedBy:

  • Date: 2020

  • Version: 1.0

  • Description:
    ***********************/
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    using DG.Tweening;
    public class CameraController_xc : MonoBehaviour
    {
    bool isRotate;
    //移动端
    private Touch oldTouch1; //上次触摸点1(手指1)
    private Touch oldTouch2; //上次触摸点2(手指2)

    public float MoveSpeed,RotateSpeed; //移动端拖动速度、旋转速度

    //pc端
    public float ScrollWheelSpeed_pc; //鼠标滚轮速度
    public float MoveSpeed_pc, RotateSpeed_pc;

    private Vector3 MouseDownPos;

    void Start()
    {
    isRotate = true;

     MoveSpeed = 3f;
     RotateSpeed = 2f;
    
     ScrollWheelSpeed_pc = 10f;
     MoveSpeed_pc = 10f;
     RotateSpeed_pc = 1f;
    

    }

    void Update()
    {
    #if UNITY_ANDROID
    //没有触摸
    if (Input.touchCount <= 0)
    {
    return;
    }
    //单点触摸
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {

         //得到手指移动的增量(只有X、Y值)
         Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
         //if (!isRotate)
         //{
         //    //相机移动
         //    transform.Translate(-touchDeltaPosition.x * Time.deltaTime * MoveSpeed,
         //        -touchDeltaPosition.y * Time.deltaTime * MoveSpeed, 0);
         //}
         //else
         //{
             //相机旋转
             if (Mathf.Abs(touchDeltaPosition.y) > Mathf.Abs(touchDeltaPosition.x))
             {
                 if (touchDeltaPosition.y > 0)
                 {
                     transform.Rotate(-touchDeltaPosition.y * Time.deltaTime * RotateSpeed, 0, 0);
                 }
                 else
                 {
                      transform.Rotate(-touchDeltaPosition.y * Time.deltaTime * RotateSpeed, 0, 0);
                 }
             }
             else
             {
                 if (touchDeltaPosition.x > 0)
                 {
                     transform.Rotate(0, touchDeltaPosition.x * Time.deltaTime * RotateSpeed, 0, Space.World);
                 }
                 else
                 {
                     transform.Rotate(0, touchDeltaPosition.x * Time.deltaTime * RotateSpeed, 0, Space.World);
                 }
             }
         //}
     }
     else if (Input.touchCount >= 1)
     {
         //多点触摸, 放大缩小 
         Touch newTouch1 = Input.GetTouch(0);
         Touch newTouch2 = Input.GetTouch(1);
    
         //第2点刚开始接触屏幕, 只记录,不做处理  
         if (newTouch2.phase == TouchPhase.Began)
         {
             oldTouch2 = newTouch2;
             oldTouch1 = newTouch1;
             return;
         }
    
         //计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型  
         float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
         float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
    
         //两个距离之差,为正表示放大手势, 为负表示缩小手势  
         float offset = newDistance - oldDistance;
         float scaleFactor = offset / 100f;
    
         transform.position += transform.localRotation * (Vector3.forward * scaleFactor);
    
    
         //记住最新的触摸点,下次使用  
         oldTouch1 = newTouch1;
         oldTouch2 = newTouch2;
     }
    

#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
//滚轮前进后退放大缩小
float scrollWheelValue = Input.GetAxis(“Mouse ScrollWheel”);

    transform.position += transform.localRotation * 
        (Vector3.forward * scrollWheelValue * ScrollWheelSpeed_pc );
    

    //wasd控制移动
    float hor = Input.GetAxis("Horizontal");
    float ver = Input.GetAxis("Vertical");
    float hor2 = Input.GetAxis("Horizontal2");
    float ver2 = Input.GetAxis("Vertical2");
    if (ver!=0||hor!=0)
    {
        transform.Translate(hor * MoveSpeed_pc * Time.deltaTime, 0, ver * MoveSpeed_pc * Time.deltaTime);
    }
    if (ver2 != 0 || hor2 != 0)
    {
        transform.Rotate(-ver2 * MoveSpeed_pc * Time.deltaTime, 0, 0);
        transform.Rotate(0, hor2 * MoveSpeed_pc * Time.deltaTime, 0, Space.World);
    }
    //右键旋转
    if (Input.GetMouseButton(1))
    {
        float h = Input.GetAxis("Mouse X");//右正左负 
        float v = Input.GetAxis("Mouse Y");//上正下负 
        if (Mathf.Abs(h) >= Mathf.Abs(v))
        {
            if (h < 0)
            {
                transform.Rotate(-Vector3.up, RotateSpeed_pc, Space.World);
            }
            if (h > 0)
            {
                transform.Rotate(Vector3.up, RotateSpeed_pc, Space.World);
            }
        }
        else
        {
            if (v < 0)
            {
                transform.Rotate(Vector3.right, RotateSpeed_pc);
            }
            if (v > 0)
            {
                transform.Rotate(-Vector3.right, RotateSpeed_pc);
            }
        }
    }

    // 鼠标中键拖动相机.
    if (Input.GetMouseButton(2))
    {
        float X = Input.GetAxis("Mouse X");
        float Y = Input.GetAxis("Mouse Y");
        //相机移动
        transform.Translate(-X * Time.deltaTime * MoveSpeed_pc,
            -Y * Time.deltaTime * MoveSpeed_pc, 0);
    }

#endif
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值