一、以摄像机为中心,右键旋转
将脚本挂载到摄像机上,以摄像机为中心旋转视野
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;