Unity右键旋转,滚轮缩放,中键平移,跟随玩家

一、以摄像机为中心,右键旋转

将脚本挂载到摄像机上,以摄像机为中心旋转视野

    private Vector2 offs;
    public float speed=2;
	void Start () {
   
        offs = transform.eulerAngles;
	}
	void Update () {
   
        if (Input.GetMouseButton(1))     //用户交互的键入
        {
   
            offs += new Vector2(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"))*speed;   //摄像机旋转的控制
            transform.eulerAngles = offs;
        }
	}

二、以物体为中心,右键旋转,滚轮缩放,中键平移

脚本挂载到摄像机上

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * 以物体target为中心
 * 右键上下左右旋转
 * 滚轮视野拉近拉远
 * 中键平移场景
 */
public class CameraController : MonoBehaviour
{
   	
	public Transform target;//相机跟随的目标物体,一般是一个空物体;没有需要为中心的物体时,为摄像机即可,target=gameObject.transform;
	private int MouseWheelSensitivity = 1; //滚轮灵敏度设置
	private int MouseZoomMin = 1; //相机距离最小值
	private int MouseZoomMax = 20; //相机距离最大值

	private float moveSpeed = 10; //相机跟随速度(中键平移时),采用平滑模式时起作用,越大则运动越平滑

	private float xSpeed = 5.0f; 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值