Search Unity

How do I capture one objects vec3 and pass it to another object transform?

Discussion in 'Scripting' started by San_Holo, Nov 15, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    I thought it would be simple but I'm stuck... I'll explain and hopefully you could enlighten me.

    I have one object which is moving slowly through screen space and at a certain time I instantiate another object/sprite over the top at it's current transform location, all is good, but during update I wanted to take the newly instantiated object transform and keep it on sync or on top of the original object so it moves with it, I thought I could do it, I am getting the location of the other object and I am instantiating the sprite over the top but I may need to bag myself the vector 3 co-ordinates in real time of the other object and pass it to the newly instantiated sprite and update it... Have I explained that OK?

    I've tried various ways and I'm a bit bamboozled...

    Does a sprite without a rigid-body component have the same ability to be positioned and updated? I suppose so... can't be that... my script simply finds a closest object (an enemy ship for example in my case) and then instantiates an image over the top, this is in Start() but I'm sorely lacking some wisdom during Update() to try and find a way to keep track of that original enemy-ship and pass it's vectors onto the overly image.

    Please advise, thanks
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Create a property in your script, assign to this property in Start (or wherever is convenient), and then use it in Update.

    It's hard for me to be more specific because I really don't understand what you're trying to do. But maybe what you want is to create a property of type Transform, let's call it "thingToFollow":

    Code (csharp):
    1. Transform thingToFollow;
    ...then you would assign it the transform of whatever you want to follow; I don't know how you're getting that, but it sounds like you have some way to do it because you said you got the location of the other object.

    Then in Update, you could make the object this script is attached to follow the other one, just like this:

    Code (csharp):
    1. transform.position = thingToFollow.position;
    (Though in fact, you may want to do this in LateUpdate instead of Update in case the other thing moves in its own Update method, which may fire before or after this one.)
     
    San_Holo likes this.
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    The code below did the trick, thanks for your help.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SeekIcon : MonoBehaviour
    5. {
    6.  
    7.      private GameObject nearestEnemy;
    8.  
    9.      void Start ()
    10.      {
    11.          foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("Enemies")) {
    12.              transform.position = enemy.transform.position;
    13.              nearestEnemy = enemy;
    14.          }
    15.      }
    16.  
    17.      void LateUpdate ()
    18.      {
    19.          transform.position = nearestEnemy.transform.position;
    20.      }
    21. }
     
    JoeStrout likes this.