码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D物体在UI界面的显示同时控制模型旋转

时间:2019-06-04 13:08:02      阅读:567      评论:0      收藏:0      [点我收藏+]

标签:update   first   put   模型   mit   unit   oom   ram   sys   

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; 
    public static ChinarSmoothUi3DCamera Instance;   
    private void Awake()
    {
        Instance = this;
    }
    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() {

        Sencoend();
        //First();
    }
    void Sencoend()
    {
         if (point == null)
         {
            return;
         }
         if (Input.GetMouseButton(1))
         {
            targetX += Input.GetAxis("Mouse X")*-1 * xSpeed * 0.02f;
         }
        x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
        Quaternion rotation = Quaternion.Euler(0, x, 0);
        point.transform.rotation = rotation;
       
    }



    void First()
    {

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

  

Unity3D物体在UI界面的显示同时控制模型旋转

标签:update   first   put   模型   mit   unit   oom   ram   sys   

原文地址:https://www.cnblogs.com/clhxxlcj/p/10972551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!