Unity 3d里的相機(jī)跟隨
固定相機(jī)跟隨
這種相機(jī)有一個(gè)參考對(duì)象,它會(huì)保持與該參考對(duì)象固定的位置,跟隨改參考對(duì)象發(fā)生移動(dòng)
using UnityEngine;
using System.Collections;
public class CameraFlow : MonoBehaviour
{
public Transform target;
private Vector3 offset;
// Use this for initialization
void Start()
{
offset = target.position - this.transform.position;
}
// Update is called once per frame
void Update()
{
this.transform.position = target.position - offset;
}
}
固定相機(jī)跟隨,帶有角度旋轉(zhuǎn)
這一種相機(jī)跟隨是對(duì)第一種相機(jī)跟隨的改進(jìn),在原有基礎(chǔ)上面,添加了跟隨角度的控制
using UnityEngine;using System.Collections;
public class CameriaTrack : MonoBehaviour {
private Vector3 offset = new Vector3(0,5,4);//相機(jī)相對(duì)于玩家的位置
private Transform target;
private Vector3 pos;
public float speed = 2;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update () {
pos = target.position + offset;
this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//調(diào)整相機(jī)與玩家之間的距離
Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//獲取旋轉(zhuǎn)角度
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);
}
}
第三人稱相機(jī)
這種相機(jī)跟隨,是第三人稱角度看向?qū)ο蟮?,也就是一直看向?qū)ο蟮暮竺妫缫恢憋@示玩家的后背
using UnityEngine;
using System.Collections;
//相機(jī)一直拍攝主角的后背
public class CameraFlow : MonoBehaviour {
public Transform target;
public float distanceUp=15f;
public float distanceAway = 10f;
public float smooth = 2f;//位置平滑移動(dòng)值
public float camDepthSmooth = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// 鼠標(biāo)軸控制相機(jī)的遠(yuǎn)近
if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
{
Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
}
}
void LateUpdate()
{
//相機(jī)的位置
Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
//相機(jī)的角度
transform.LookAt(target.position);
}
}
相機(jī)跟隨,鼠標(biāo)控制移動(dòng)和縮放
相機(jī)與觀察對(duì)象保持一定距離,可以通過(guò)鼠標(biāo)進(jìn)行上下左右旋轉(zhuǎn),通過(guò)鼠標(biāo)滾輪進(jìn)行放大和縮小操作
using UnityEngine;
using System.Collections;
public class CameraFlow : MonoBehaviour
{
public Transform target;
Vector3 offset;
// Use this for initialization
void Start()
{
offset = transform.position - target.position;
}
// Update is called once per frame
void Update()
{
transform.position = target.position + offset;
Rotate();
Scale();
}
//縮放
private void Scale()
{
float dis = offset.magnitude;
dis += Input.GetAxis("Mouse ScrollWheel") * 5;
Debug.Log("dis=" + dis);
if (dis < 10 || dis > 40)
{
return;
}
offset = offset.normalized * dis;
}
//左右上下移動(dòng)
private void Rotate()
{
if (Input.GetMouseButton(1))
{
Vector3 pos = transform.position;
Vector3 rot = transform.eulerAngles;
//圍繞原點(diǎn)旋轉(zhuǎn),也可以將Vector3.zero改為 target.position,就是圍繞觀察對(duì)象旋轉(zhuǎn)
transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
float x = transform.eulerAngles.x;
float y = transform.eulerAngles.y;
Debug.Log("x=" + x);
Debug.Log("y=" + y);
//控制移動(dòng)范圍
if (x < 20 || x > 45 || y < 0 || y > 40)
{
transform.position = pos;
transform.eulerAngles = rot;
}
// 更新相對(duì)差值
offset = transform.position - target.position;
}
}
}
- 上一篇:Unity 3D坐標(biāo)系轉(zhuǎn)換 2021/2/26
- 下一篇:UNITY3D 與 HTC VIVE 數(shù)據(jù)手套組合應(yīng)用VR開(kāi) 2021/2/24


