模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)

问题:

          怎么让当手指滑动的同时对应的模型发生旋转

解决办法:

          1:通过控制摄像机或者模型来实现效果

          2:通过获取鼠标移动时X轴Y轴的偏移量来确定模型的旋转角度

          3:为了不让人感觉到突兀,建议使用Mathf.SmoothDamp方法实现角度的改变

 

实现代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChinarSmoothUi3DCamera : MonoBehaviour {

    public Transform point;
    private Vector3 Tras = Vector3.zero;

    public float distance = 10.0f;
    public float minDistance = 2f;
    public float maxDistance = 15f;
    public float zoomSpeed = 1f;
    public float xSpeed = 250.0f;
    public float ySpeed = 250.0f;
    public bool allowYTilt = true;
    public float yMinLimit = -90f;
    public float yMaxLimit = 90f;

    private float x = 0.0f;
    private float y = 0.0f;
    private float targetX = 0f;
    private float targetY = 0f;
    public float targetDistance = 0f;
    private float xVelocity = 1f;
    private float yVelocity = 1f;
    private float zoomVelocity = 1f;
    void Start () {

        Vector3 tange = transform.eulerAngles;
        targetX = x = tange.x;
        //targetY = y = ClampAngle(tange.y, yMinLimit, yMaxLimit);       
        targetDistance = distance;


    }

    // Update is called once per frame
    
    void LateUpdate() {
		if(point==null)
        {
            return;
        }
        if(Input.GetAxis("Mouse ScrollWheel")>0)
        {
            targetDistance -= zoomSpeed;
        }
        if(Input.GetAxis("Mouse ScrollWheel")<0)
        {
            targetDistance += zoomSpeed;
        }
        targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);
        if(Input.GetMouseButton(1))
        {
            
            targetX+= Input.GetAxis("Mouse X") * xSpeed * 0.02f;           
            if (allowYTilt)
            {
                //targetY -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f;
                //targetY= ClampAngle(targetY, yMinLimit, yMaxLimit);
            }
        }
       x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
        //y = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY;
        //Quaternion rotation = Quaternion.Euler(y, x, 0);
        Quaternion rotation = Quaternion.Euler(0, x, 0);
        distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);
        Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + point.position + Tras;
        transform.rotation = rotation;
        transform.position = position;
        

    }

    public float ClampAngle(float angle,float min,float max)
    {
        if(angle>360)
        {
            angle -= 360;
        }
        if(angle<-360)
        {
            angle += 360;
        }
        return Mathf.Clamp(angle, min, max);
    }
}

  

转载于:https://www.cnblogs.com/clhxxlcj/p/10916720.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值