2021年3月8日 星期一

跟隨物件

可使用子彈跟蹤
----------------------------

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

public class Bullet_Missile : MonoBehaviour
{

    private Transform targetTr;   // 跟隨物件
    public float dist = 5f;
    public float height = 5.0f;
    public float dampTrace = 10.0f;

    // Update is called once per frame
    void Update()
    {
        targetTr = GameObject.Find("Planet").transform;

        transform.position = Vector3.Lerp(transform.position, targetTr.position - (targetTr.forward * dist) + (Vector3.up * height), Time.deltaTime * dampTrace);
        transform.LookAt(targetTr.position);
    }
}