Home > other >  How do i get one object to track and move towards another object
How do i get one object to track and move towards another object

Time:10-08

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

public class EnemyControler : MonoBehaviour
{
    private Transform player;
    // Start is called before the first frame update
    void Start()
    {

        //Note in this case we're saving the transform in a variable to work with later. 

        //We could save the player's game object (and just remove .transform from the below) but this is cleaner
       var player = GameObject.FindWithTag("Player").transform;
    }

    // Update is called once per frame
    void Update()
    {

        //**** The code below you need to modify and move into your own method and call that method from here.

        //**** Don't overthink it. Look at how Start() is defined. Your own Move method would look the exact same.

        //**** Experiment making it private void instead of public void. It should work the same.

        //How much will we move by after 1 second? .5 meter
        var step = .5f * Time.deltaTime; // 5 m/s

        var newPosition = Vector3.MoveTowards(transform.position, player, step);//This is one way to move a game object by updating the transform. Watch the videos in this module and it should become apparent what goes here.
    }
}

The transform does not map to the tagged player. I was trying to get an enemy in unity to move towards a moving player for a class, but I'm completely lost.

CodePudding user response:

Try doing something like this ive used this in the past to do something similar once you understand the code change it to what you desire

        public int minRange;
        private Transform target = null;
        
            private void OnTriggerEnter(Collider other)
            {
                if (other.tag == "Player")
                    target = other.transform;
            }
        
            private void OnTriggerExit(Collider other)
            {
                if (other.tag == "Player")
                    target = null;
            }   
        
            void update()
            {
                transform.LookAt(target);
                float distance = Vector3.Distance(transform.position, 
                target.position);
                bool tooClose = distance < minRange;
                Vector3 direction = tooClose ? Vector3.back : Vector3.forward;
                if (direction == Vector3.forward)
                {
                    transform.Translate(direction * 6 * Time.deltaTime);
                }
                else
                {
                    transform.Translate(direction * 3 * Time.deltaTime);
                 }
            }

CodePudding user response:

You had three issues in your code. The first was that you were defining a new player variable in the Start method. This hid the player member field you defined earlier.

The second issue was that you were getting a value to move towards, but you weren't assigning that value to the current objects position.

The third issue was that you were feeding in a Transform into the MoveTowards method, when you should have been feeding in a Vector2.

The code below should rectify these issues.

public class EnemyControler : MonoBehaviour
{
    private Transform _player;

    void Start()
    {
       _player = GameObject.FindWithTag("Player").transform;
    }

    void Update()
    {
        var step = .5f * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, _player.position, step);
    }
}
  • Related