Help us understand the problem. What is going on with this article?

【Unity】対象を追跡する

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

// ずっと、追いかける
public class Forever_Chase : MonoBehaviour
{
    public string targetObjectName;   // 目標オブジェクト名
    public float speed = 1; // スピード:Inspectorで指定
    GameObject targetObject;

    void Start () // 最初に、目標オブジェクトを見つけておく
    {
        targetObject = GameObject.Find(targetObjectName);
    }

    private void FixedUpdate() // ずっと、目標オブジェクトの方向を調べて
    {
        Vector3 dir = (targetObject.transform.position - this.transform.position).normalized;
        // その方向へ指定した量で進む
        float vx = dir.x * speed;
        float vz = dir.z * speed;
        this.transform.Translate(vx / 50, 0,  vz / 50);
    }
}

まず下記で、追跡する対象の方向を求める
Vector3 dir = (targetObject.transform.position - this.transform.position).normalized;

そのあと、求めたx軸とz軸方向に、速度の値をかける。

ezgif.com-video-to-gif (2).gif

Unity_mametarou
Unityでゲーム制作してます 備忘録、成果物など発信していきたいと思っております!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした