Unity攝像機(jī)跟隨
方法一:攝像機(jī)子物體
將攝像機(jī)直接拖拽到被跟隨的目標(biāo)下面即可,這樣攝像機(jī)永遠(yuǎn)在目標(biāo)的后面文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-832454.html
缺點(diǎn):
- 屏幕旋轉(zhuǎn)太平滑了
- 目標(biāo)物體在屏幕上的位置永遠(yuǎn)不變
- 目標(biāo)物體被銷毀時(shí)總不能把攝像機(jī)也銷毀了吧
方法二:子物體加向量得到攝像機(jī)位置
先相機(jī)坐標(biāo)和物體坐標(biāo)做差,求得偏移量,在之后的每一幀里,將偏移量加上物體的坐標(biāo)。
需要注意的是,理想中的相機(jī)位置,應(yīng)該是在物體本地坐標(biāo)系上加上偏移量,所以我們需要將這個(gè)偏移量假設(shè)成本地坐標(biāo)系下的,然后轉(zhuǎn)換成世界坐標(biāo)系,再進(jìn)行相加
此時(shí)可以正確跟隨,但是會(huì)比較僵硬,所以我們使用插值對(duì)相機(jī)現(xiàn)在的位置和目標(biāo)位置進(jìn)行插值。
最后讓相機(jī)一直看向物體即可。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-832454.html
public Transform player_transform;
private Vector3 offset;
public float smooth;
void Start()
{
offset = this.transform.position - player_transform.position;
smooth = 3;
}
void LateUpdate()
{
Vector3 target_position = player_transform.position + player_transform.TransformDirection(offset);
transform.position = Vector3.Lerp(transform.position, target_position, Time.deltaTime * smooth);
transform.LookAt(player_transform);
}
到了這里,關(guān)于Unity攝像機(jī)跟隨的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!