iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1091229 讓物件跟隨目標
by 黃國哲 2020-12-29 18:48:39, Reply(0), Views(812)

www.itread01.com/content/1549613550.html

使用以下代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class followCam : MonoBehaviour {
public Transform targetTr;
public float dist = 5f;
public float height = 5.0f;
public float dampTrace = 10.0f;

public Transform tr;
// Use this for initialization
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);
}
}
Reply