unity——控制攝像機(jī)的腳本

攝像機(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);
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末词顾,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摔桦,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)穿挨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肴盏,“玉大人科盛,你說我怎么就攤上這事〔嗽恚” “怎么了贞绵?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長恍飘。 經(jīng)常有香客問我榨崩,道長,這世上最難降的妖魔是什么常侣? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任蜡饵,我火速辦了婚禮,結(jié)果婚禮上胳施,老公的妹妹穿的比我還像新娘溯祸。我一直安慰自己,他們只是感情好舞肆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布焦辅。 她就那樣靜靜地躺著,像睡著了一般椿胯。 火紅的嫁衣襯著肌膚如雪筷登。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天哩盲,我揣著相機(jī)與錄音前方,去河邊找鬼狈醉。 笑死,一個(gè)胖子當(dāng)著我的面吹牛惠险,可吹牛的內(nèi)容都是我干的苗傅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼班巩,長吁一口氣:“原來是場噩夢啊……” “哼渣慕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起抱慌,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤逊桦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后抑进,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體强经,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年单匣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夕凝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡户秤,死狀恐怖码秉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鸡号,我是刑警寧澤转砖,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站鲸伴,受9級特大地震影響府蔗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜汞窗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一姓赤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧仲吏,春花似錦不铆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至许帐,卻和暖如春劳坑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背成畦。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工距芬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涝开,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓蔑穴,卻偏偏與公主長得像忠寻,于是被迫代替她去往敵國和親惧浴。 傳聞我的和親對象是個(gè)殘疾皇子存和,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容