相機跟隨(基礎)
本文為轉載文章,感謝原博主的精彩分享,本人知識有限,只能借寶獻醜
固定相機跟隨
這種相機有一個參考物件,它會保持與該參考物件固定的位置,跟隨改參考物件發生移動
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;
}
}1234567891011121314151617181920
固定相機跟隨,帶有角度旋轉
這一種相機跟隨是對第一種相機跟隨的改進,在原有基礎上面,添加了跟隨角度的控制
using UnityEngine;
using System.Collections;
public class CameriaTrack : MonoBehaviour {
private Vector3 offset = new Vector3(0,5,4);//相機相對於玩家的位置
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);//調整相機與玩家之間的距離
Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//獲取旋轉角度
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);
}
}
1234567891011121314151617181920212223242526
第三人稱相機
這種相機跟隨,是第三人稱角度看向物件的,也就是一直看向物件的後面,如一直顯示玩家的後背
using UnityEngine;
using System.Collections;
//相機一直拍攝主角的後背
public class CameraFlow : MonoBehaviour {
public Transform target;
public float distanceUp=15f;
public float distanceAway = 10f;
public float smooth = 2f;//位置平滑移動值
public float camDepthSmooth = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// 滑鼠軸控制相機的遠近
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()
{
//相機的位置
Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
//相機的角度
transform.LookAt(target.position);
}
}
相機跟隨,滑鼠控制移動和縮放
相機與觀察物件保持一定距離,可以通過滑鼠進行上下左右旋轉,通過滑鼠滾輪進行放大和縮小操作
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;
}
//左右上下移動
private void Rotate()
{
if (Input.GetMouseButton(1))
{
Vector3 pos = transform.position;
Vector3 rot = transform.eulerAngles;
//圍繞原點旋轉,也可以將Vector3.zero改為 target.position,就是圍繞觀察物件旋轉
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);
//控制移動範圍
if (x < 20 || x > 45 || y < 0 || y > 40)
{
transform.position = pos;
transform.eulerAngles = rot;
}
// 更新相對差值
offset = transform.position - target.position;
}
}
}
---------------------
作者:塵世喧囂
來源:CSDN
原文:https://blog.csdn.net/u011484013/article/details/51554745
版權宣告:本文為博主原創文章,轉載請附上博文連結!