攝像機(jī)跟隨鼠標(biāo)移動(dòng)
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;
}
另一個(gè)控制腳本
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
}
}
第三人稱攝像機(jī)跟隨
using UnityEngine;
using System.Collections;
public class CamerFllow : MonoBehaviour
{
public Transform target;//要跟隨的目標(biāo)
public float distance = 8.0f;//攝像機(jī)離目標(biāo)的距離
public float height = 5.0f;//攝像機(jī)離目標(biāo)的高度
public float heihtDamping = 0.3f;//水平跟隨平滑系數(shù)
public float rotationDamping = 0.3f;//跟隨高度變化系數(shù)
public float refRotation = 0f;
public float refHeight = 0f;
void LateUpdate()
{
if (target)
{
float targetRotationAngle = target.eulerAngles.y;//目標(biāo)的朝向
float targetHeight = target.position.y + height;//得到跟隨的高度
float cameraRotationAngle = transform.eulerAngles.y;//攝像機(jī)的朝向
float cameraHeight = transform.position.y;//攝像機(jī)的高度
cameraRotationAngle = Mathf.SmoothDampAngle(cameraRotationAngle, targetRotationAngle, ref refRotation, rotationDamping);//從攝像機(jī)目前的角度變換到目標(biāo)的角度
cameraHeight = Mathf.SmoothDamp(cameraHeight, targetHeight, ref refHeight, heihtDamping);//從攝像機(jī)目前的高度平滑變換到目標(biāo)的高度
Quaternion cameraRotation = Quaternion.Euler(0, cameraRotationAngle, 0);//每幀在Y軸上旋轉(zhuǎn)攝像機(jī) 旋轉(zhuǎn)的角度為cameraRotationAngle 因?yàn)樯厦娴拇a已經(jīng)得到了每幀要從攝像機(jī)當(dāng)前的角度變換到目標(biāo)角度cameraRotationAngle
//下面幾句代碼主要設(shè)置攝像機(jī)的位置
transform.position = target.position;
transform.position -= cameraRotation * Vector3.forward * distance;
transform.position = new Vector3(transform.position.x, cameraHeight, transform.position.z);
//使攝像機(jī)一直朝著目標(biāo)方向
transform.LookAt(target);
}
}
}