1. 程式人生 > >Unity 攝像機跟隨

Unity 攝像機跟隨

cti 第一個 攝像 war oat ner trac 遊戲對象 trace

方式一:將攝像機直接拖到遊戲對象的下面;

方式二:腳本實現

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class kow : MonoBehaviour
{
    public Transform targetTr;
    public float dist = 10.0F;
    public float height = 3.0F;
    public float dampTrace = 20.0F;

    private
Transform tr; // Start is called before the first frame update void Start() { tr = GetComponent<Transform>(); } // Update is called once per frame void LateUpdate() { tr.position = Vector3.Lerp(tr.position, targetTr.position - (targetTr.forward * dist) + (Vector3.up * height), Time.deltaTime * dampTrace); tr.LookAt(targetTr.position); } }

這裏主要的是Vector3.Lerp()和函數,三個參數,第一個:起始位置;第二個:結束位置;第三個:浮點格式。

Unity 攝像機跟隨