unity——控制摄像机的脚本

摄像机跟随鼠标移动

var rSpeed = 3.0;
var mSpeed = 20.0;
var X = 0.0;
var Y = 0.0;

function Update (){

    X += Input.GetAxis("Mouse X")*rSpeed;    
    Y += Input.GetAxis("Mouse Y")*rSpeed; 
    transform.localRotation = Quaternion.AngleAxis(X, Vector3.up);
    transform.localRotation *= Quaternion.AngleAxis(Y, Vector3.left);
    transform.position += transform.forward*mSpeed*Input.GetAxis("Vertical")*Time.deltaTime;
    transform.position += transform.right*mSpeed*Input.GetAxis("Horizontal")*Time.deltaTime;
}
另一个控制脚本
using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {

    private float camSpeed= 0.50f;
    
    void Start()
    {
        SetPosition(); // Set the initial position of the camera, this needed just for this demonstation
    }
    
    void  LateUpdate ()
    {
        if (Input.mousePosition.y <= 2) // If the mouse is at the bottom of the visible screen or lower then continue
        {
            if(transform.position.z >= -25f) // If the camera is within desired Z axis then continue
            {
                transform.Translate(0, 0, -camSpeed, Space.World); // this moves the camera down based on the worlds vectors
            }
        }
        if (Input.mousePosition.y >= Screen.height -2) // If the mouse is at the top of the visible screen or higher then continue
        {
            if(transform.position.z <= 15) // If the camera is within desired Z axis then continue
            {
                transform.Translate(0, 0, camSpeed, Space.World); // this moves the camera up based on the worlds vectors
            }
        }
        if (Input.mousePosition.x <= 2) // If the mouse is at the left of the visible screen or lower then continue
        {
            if(transform.position.x >= -25f) // If the camera is within desired X axis then continue
            {
                transform.Translate(-camSpeed, 0, 0, Space.World); // this moves the camera left based on the worlds vectors
            }
        }
        if (Input.mousePosition.x >= Screen.width -2) // If the mouse is at the right of the visible screen or higher then continue
        {
            if(transform.position.x <= 25)  // If the camera is within desired X axis then continue
            {
                transform.Translate(camSpeed, 0, 0, Space.World); // this moves the camera right based on the worlds vectors
            }
        }
        if(Input.GetAxis("Mouse ScrollWheel") < 0 && Camera.main.transform.position.y <= 15) // If the mouse scroll wheel is scrolled down and not scrolled further than a number then continue
        {
            transform.Translate(0, 0, -camSpeed); // move the camera back on the z axis
        }
        if(Input.GetAxis("Mouse ScrollWheel") > 0  && Camera.main.transform.position.y >= 5) // If the mouse scroll wheel is scrolled up and not scrolled further than a number then continue
        {
            transform.Translate(0, 0, camSpeed);// move the camera forward on the z axis
        }
    }
    
    private void SetPosition()
    {
        transform.rotation = Quaternion.Euler(40f, 0, 0); // Set the camera's rotation
    }
}

第三人称摄像机跟随

using UnityEngine;
using System.Collections;

public class CamerFllow : MonoBehaviour
{

    public Transform target;//要跟随的目标
    public float distance = 8.0f;//摄像机离目标的距离
    public float height = 5.0f;//摄像机离目标的高度
    public float heihtDamping = 0.3f;//水平跟随平滑系数
    public float rotationDamping = 0.3f;//跟随高度变化系数
    public float refRotation = 0f;
    public float refHeight = 0f;

    void LateUpdate()
    {
        if (target)
        {
            float targetRotationAngle = target.eulerAngles.y;//目标的朝向
            float targetHeight = target.position.y + height;//得到跟随的高度

            float cameraRotationAngle = transform.eulerAngles.y;//摄像机的朝向
            float cameraHeight = transform.position.y;//摄像机的高度

            cameraRotationAngle = Mathf.SmoothDampAngle(cameraRotationAngle, targetRotationAngle, ref refRotation, rotationDamping);//从摄像机目前的角度变换到目标的角度

            cameraHeight = Mathf.SmoothDamp(cameraHeight, targetHeight, ref refHeight, heihtDamping);//从摄像机目前的高度平滑变换到目标的高度

            Quaternion cameraRotation = Quaternion.Euler(0, cameraRotationAngle, 0);//每帧在Y轴上旋转摄像机 旋转的角度为cameraRotationAngle 因为上面的代码已经得到了每帧要从摄像机当前的角度变换到目标角度cameraRotationAngle

            //下面几句代码主要设置摄像机的位置
            transform.position = target.position;
            transform.position -= cameraRotation * Vector3.forward * distance;
            transform.position = new Vector3(transform.position.x, cameraHeight, transform.position.z);

            //使摄像机一直朝着目标方向
            transform.LookAt(target);
        }
    }
}

你可能感兴趣的:(unity——控制摄像机的脚本)