Moving Sprite to nearest Spawned Class

In summary I’m using this class to move a sprite (that’s already in the scene) to automatically teleport and destroy the nearest spawned object.

What’s supposed to happen is the sprite has to move and destroy the nearest spawned object before stopping (since I have to add more code for it to repeat the process later)

Right now, the sprite is just stationary while the objects spawn, that’s it.

If there isn’t enough context then I can add code for other classes

The code for the “Collector.cs” class is below. The “SetTarget” and the “GoToTargetPickup” methods are supposed to achieve this effect. Anyone know the solution to this?

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


/// <summary>
/// A collecting game object
/// </summary>
public class Collector : MonoBehaviour
{
    #region Fields

    // targeting support
    SortedList<Target> targets = new SortedList<Target>();
    Target targetPickup = null;

    // movement support
    const float BaseImpulseForceMagnitude = 2.0f;
    const float ImpulseForceIncrement = 0.3f;
 
    // saved for efficiency
    Rigidbody2D rb2d;
 

    #endregion

    #region Methods

    /// <summary>
    /// Use this for initialization
    /// </summary>
    void Start()
    {
        // center collector in screen
        Vector3 position = transform.position;
        position.x = 0;
        position.y = 0;
        position.z = 0;
        transform.position = position;

        // save reference for efficiency
        rb2d = GetComponent<Rigidbody2D>();

        // add as listener for pickup spawned event
        EventManager.AddListener(DelegateMethod);

    }

    private void DelegateMethod(GameObject arg0)
    {
        print("Pickup Spawned");
    }







    /// <summary>
    /// Called when another object is within a trigger collider
    /// attached to this object
    /// </summary>
    /// <param name="other"></param>
    void OnTriggerStay2D(Collider2D other)
    {
        // only respond if the collision is with the target pickup
        if (other.gameObject == targetPickup.GameObject)
        {
            // remove collected pickup from list of targets and game
        
        
            // go to next target if there is one
        
        }
    }

 
    /// <summary>
    /// Sets the target pickup to the provided pickup
    /// </summary>
    /// <param name="pickup">Pickup.</param>
 
    void SetTarget(GameObject pickup)
    {
        targetPickup = pickup;
        GoToTargetPickup();
    }
 
    /// <summary>
    /// Starts the teddy bear moving toward the target pickup
    /// </summary>
    void GoToTargetPickup()
    {
        // calculate direction to target pickup and start moving toward it
        Vector2 direction = new Vector2(
            targetPickup.transform.position.x - transform.position.x,
            targetPickup.transform.position.y - transform.position.y);
        direction.Normalize();
        rb2d.velocity = Vector2.zero;
        rb2d.AddForce(direction * BaseImpulseForceMagnitude,
            ForceMode2D.Impulse);
    }
 
 
    #endregion
}

Bump

You need these parts:

  • the sprite (sounds like you have it)
  • a way of knowing WHAT the target destination object is
  • a way of knowing WHEN to move to that destination
  • and then it is just simply copying the position over to the moving sprite.

I do want the sprite to just teleport there, the question is how to do so

// if this is where you want to go:
Transform destination;

// and this is your sprite:
Transform mySprite;

// then a teleport is just:
mySprite.position = destination.position;

I put that in the GoToTargetPickup method, doesn’t work

Time to start debugging!

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.