U3D,鼠标控制摄像头和游戏对象

本文介绍如何使用Unity实现两种鼠标控制方式:一是利用鼠标右键控制摄像机围绕目标物体旋转并调整视角距离;二是通过鼠标左键拖动游戏对象进行旋转。详细解释了相关代码的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.鼠标右键控制摄像头

右键按住鼠标使摄像头围绕目标物体旋转,鼠标滑轮控制拉远拉近,有阻尼效果。


using UnityEngine;
using System.Collections;

public class TestCube : MonoBehaviour
{

    // 围绕旋转的目标物体  
    public Transform target;

    // 设置旋转角度  
    public float x = 0f, y = 0f, z = 0f;
    public bool xFlag = false, yFlag = false;

    // 旋转速度值  
    public float xSpeed = 100, ySpeed = 100, mSpeed = 50;

    // y轴角度限制,设置成一样则该轴不旋转  
    public float yMinLimit = -365, yMaxLimit = 365;

    // x轴角度限制,同上  
    public float leftMax = -365, rightMax = 365;

    // 距离限制,同上  
    public float distance = 3f, minDistance = 1f, maxDistance = 6f;

    // 阻尼设置  
    public bool needDamping = true;
    public float damping = 3f;

    // 改变中心目标物体  
    public void SetTarget(GameObject go)
    {
        target = go.transform;
    }

    // Use this for initialization  
    void Start()
    {
        //Vector3 angles = transform.eulerAngles;  
        //x = angles.y;  
        //y = angles.x;  
        pers();
    }

    /** 
     * Update is called once per frame 
     *  
     * 中心物体不为空,则开始运行脚本; 
     *  
     * 当有鼠标点击事件后,改变x、y的值; 
     * 当有鼠标滚轮事件后,改变distance的值; 
     *  
     * 最后,计算角度和坐标等,移动摄像机到该位置 
     *  
     */
    void LateUpdate()
    {
        if (target)
        {
            if (Input.GetMouseButton(1))
            {
                // 判断是否需要反向旋转  
                if ((y > 90f && y < 270f) || (y < -90 && y > -270f))
                {
                    x -= Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                }
                else
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                }
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                x = ClampAngle(x, leftMax, rightMax);
                y = ClampAngle(y, yMinLimit, yMaxLimit);
            }

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

            Quaternion rotation = Quaternion.Euler(y, x, z);
            Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
            Vector3 position = rotation * disVector + target.position;

            // 阻尼感  
            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;
            }

        }
    }

    // 对数值进行限制;  
    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);
    }

    // 初始  
    public void pers()
    {
        this.x = 35f;
        this.y = 35f;
    }

    // 正视图  
    public void front()
    {
        this.x = 0f;
        this.y = 0f;
    }

    // 后视图  
    public void back()
    {
        this.x = 180f;
        this.y = 0f;
    }

    // 左视图  
    public void left()
    {
        this.x = 90f;
        this.y = 0f;
    }

    // 右视图  
    public void right()
    {
        this.x = 270f;
        this.y = 0f;
    }

    // 俯视图  
    public void top()
    {
        this.x = 0f;
        this.y = 90f;
    }

    // 仰视图  
    public void bottom()
    {
        this.x = 0f;
        this.y = -90f;
    }

}


2.鼠标左键游戏对象使其旋转

using UnityEngine;
using System.Collections;

public class DragRotateWithSlider : MonoBehaviour {
	
	private bool onDrag = false;	//是否被拖拽
	public float speed = 5f;	//旋转速度
	private float tempSpeed;	//阻尼速度
	private float axisX;	//鼠标沿水平方向移动的增量
	private float axisY;	//鼠标沿垂直方向移动的增量
	private float cXY;		//鼠标移动的距离


	//接收鼠标按下的事件
	void OnMouseDown () 
	{
		axisX = 0f;	//为移动的增量赋初值
		axisY = 0f;
	}

	//鼠标拖拽时的操作
	void OnMouseDrag()
	{
		onDrag = true;	//被拖拽
		axisX = Input.GetAxis("Mouse Y");	//获得鼠标增量
		axisY = -Input.GetAxis("Mouse X");	
		cXY = Mathf.Sqrt(axisX * axisX + axisY * axisY);	//计算鼠标移动的长度
		if (cXY == 0f)
		{
			cXY = 1f;
		}
	}


	//Count TempSpeed
	float Rigid()	//计算阻尼速度
	{
		if (onDrag)
		{
			tempSpeed = speed;
		}
		else
		{
			if (tempSpeed > 0)
			{
				tempSpeed -= speed * 2 * Time.deltaTime / cXY;//通过除以鼠标移动长度实现拖拽越长速度减缓越慢
			}
			else
			{
				tempSpeed = 0;
			}
		}
		return tempSpeed;	//返回阻尼的值
	}


	void Update()
	{
		gameObject.transform.Rotate(new Vector3(axisX, axisY, 0) * Rigid(), Space.World);
		if (!Input.GetMouseButton(0))
		{
			onDrag = false;
		}
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值