您好,登錄后才能下訂單哦!
using UnityEngine;
using System.Collections;
//攝像機(jī)跟隨
public class FollowPlayer : MonoBehaviour {
private Transform player;//角色
private Vector3 offsetPosition;//位置便宜
private bool isRotating=false;//是否有在滑動(dòng)
public float distance;//向量長(zhǎng)度
public float scrollSpeed=3;//拉近拉遠(yuǎn)的速度
public float rotareSpeed=2;//旋轉(zhuǎn)的速度
void Start ()
{
player=GameObject.FindGameObjectWithTag(Tags.player).transform;
transform.LookAt(player.position);
offsetPosition=transform.position-player.position;//得到偏移量
}
void Update ()
{
transform.position=offsetPosition+player.position;
RotateView();
ScrollView();
}
//處理視野的拉近和拉遠(yuǎn)效果
void ScrollView()
{
// print(Input.GetAxis("Mouse ScrollWheel"));//鼠標(biāo)向后滑動(dòng)返回負(fù)數(shù)(拉近視野),向前正數(shù)(拉遠(yuǎn)視野)
distance = offsetPosition.magnitude;
distance += Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
distance=Mathf.Clamp(distance,2,18);
offsetPosition=offsetPosition.normalized*distance;//改變位置便移
}
//控制視野左右上下旋轉(zhuǎn)
void RotateView()
{
//Input.GetAxis("Mouse X");//得到鼠標(biāo)水平方向的滑動(dòng)
//Input.GetAxis("Mouse Y");//得到鼠標(biāo)在垂直方向的滑動(dòng)
if(Input.GetMouseButtonDown(1))
{
isRotating=true;
}
if(Input.GetMouseButtonUp(1))
{
isRotating=false;
}
if(isRotating)
{
transform.RotateAround(player.position,player.up, rotareSpeed*Input.GetAxis("Mouse X"));//圍撈角色滑動(dòng) 左右
Vector3 originalPos=transform.position;
Quaternion originalRotation=transform.rotation;
transform.RotateAround(player.position,transform.right,-rotareSpeed*Input.GetAxis("Mouse Y"));//上下 (會(huì)影響到的屬性一個(gè)是Position,一個(gè)是rotation)
//限制上下滑動(dòng)的度數(shù)大小
float x=transform.eulerAngles.x;
if(x<10||x>80)//當(dāng)超出范圍之后,我們將屬性歸位,讓旋轉(zhuǎn)無效
{
transform.position=originalPos;
transform.rotation=originalRotation;
}
}
offsetPosition=transform.position-player.position;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。