unity 点击鼠标左键控制物体360展示

本文介绍如何在Unity中通过点击鼠标左键来控制物体进行360度全方位展示,详细阐述了实现过程和技术要点。
摘要由CSDN通过智能技术生成

原文地址:https://blog.csdn.net/lhj1101049257/article/details/43734181 

/***
*	Title:"智慧工厂" 项目
*		主题:控制鼠标移动物体
*	Description:
*		功能:点击鼠标左键控制物体360展示
*	Date:2018
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

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

namespace SimpleUIFrame
{
	public class Global_FllowMouseRotate : MonoBehaviour
	{

        public Transform target;                                                //目标物体
        public float xSpeed = 200, ySpeed = 200, mSpeed = 10;                   //移动速度
        public float yMinLimit = -50, yMaxLimit = 50;                           //摄像机的Y轴移动最小最大限制
        public float distance = 7, minDistance = 2, maxDistance = 30;           //摄像机与目标物体的距离

        
        public bool needDamping = true;                                         //阻尼默认开启
        float damping = 5.0f;                                                   //默认阻尼为5.0F

        public float x = 0.0f;                                                  //X轴
        public float y = 0.0f;                                                  //Y轴

        //初始化
        void Start()
        {
            Vector3 angles = transform.eulerAngles;
            x = angles.y;
            y = angles.x;
        }

        /// <summary>
        /// 检测鼠标是否按下进行旋转物体
        /// </summary>
        void LateUpdate()
        {
            if (target)
            {
                //使用按下鼠标左键移动物体
                if (Input.GetMouseButton(0))
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                    y = ClampAngle(y, yMinLimit, yMaxLimit);
                }


                distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
                distance = Mathf.Clamp(distance, minDistance, maxDistance);


                Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
                Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
                Vector3 position = rotation * disVector + target.position;
                //adjust the camera
                if (needDamping)
                {
                    transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                    transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
                }
                else
                {
                    transform.rotation = rotation;
                    transform.position = position;
                }


            }
        }

        /// <summary>
        /// 旋转角度的控制
        /// </summary>
        /// <param name="angle">旋转的角度</param>
        /// <param name="min">最小角度</param>
        /// <param name="max">最大角度</param>
        /// <returns></returns>
        static float ClampAngle(float angle, float min, float max)
        {
            if (angle < -360)
                angle += 360;
            if (angle > 360)
                angle -= 360;
            return Mathf.Clamp(angle, min, max);
        }


    }//class_end
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值