敵が一定の距離に近づいたらロックオンし砲弾を発射(Distanceの活用)

Vector3.Distanceの活用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RadarShot : MonoBehaviour
{
    private GameObject target;
    public GameObject shellPrefab;
    public AudioClip sound;
    private int count;
    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player");
    }
    void Update()
    {
        // ターゲットとの距離を取得する。
        if(Vector3.Distance(transform.position, target.transform.position) <= 10f)
        {
            // 親をターゲットの方に向かせる。
            transform.root.LookAt(target.transform.position);
            count += 1;
            if (count % 10 == 0)
            {
                GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
                Rigidbody shellRb = shell.GetComponent<Rigidbody>();
                shellRb.AddForce(transform.forward * 500);
                AudioSource.PlayClipAtPoint(sound, transform.position);
                Destroy(shell, 3.0f);
            }
        }
    }
}Unity Code Memo
他のコースを見る
Vector3.Distanceの活用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RadarShot : MonoBehaviour
{
    private GameObject target;
    public GameObject shellPrefab;
    public AudioClip sound;
    private int count;
    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player");
    }
    void Update()
    {
        // ターゲットとの距離を取得する。
        if(Vector3.Distance(transform.position, target.transform.position) <= 10f)
        {
            // 親をターゲットの方に向かせる。
            transform.root.LookAt(target.transform.position);
            count += 1;
            if (count % 10 == 0)
            {
                GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
                Rigidbody shellRb = shell.GetComponent<Rigidbody>();
                shellRb.AddForce(transform.forward * 500);
                AudioSource.PlayClipAtPoint(sound, transform.position);
                Destroy(shell, 3.0f);
            }
        }
    }
}敵が一定の距離に近づいたらロックオンし砲弾を発射(Distanceの活用)