"Teleportation" script issue

Hi guys.

i’m having a problem with my script. What it does is basically look inside of a GameObject array for each elements position, then determinate the furthest one from the origin, and make the object this script is attached to translate to the furthest object position. But it won’t work, i’ve got a few different error messages already, but i won’t find the problem. Could you guys help me with this?

thanks for your attention.

using UnityEngine;
using System.Collections;

public class zuera : MonoBehaviour {

   public  GameObject[] List = new GameObject[2];
    public Vector3 maisDistanteVec = new Vector3();
    public int mostDistant = 0;
    public float vectorSize;
    private Vector3 mostDistantOverall = new Vector3();
    public Vector3 myPos = new Vector3();
    private Vector3 resultantVector = new Vector3();

    void Start()
    {
        myPos = this.transform.position;
    }

    void Update () {


        for (int i =0; i < List.Length; i++)
        {
            vectorSize = List[i].transform.position.x + List[i].transform.position.y + List[i].transform.position.z;

            if (vectorSize > mostDistant)
            {
                mostDistantOverall = List[i].transform.position;
            }

            resultantVector = mostDistantOverall - myPos;

            transform.Translate(resultantVector);

        }
    }
}

Hi Bruno_AMS,

Instead of Translating to your desired position, try setting the position of the transform equal to the position of the target using transform.position. Below is an small example, and a link to the Unity Docs for more information.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {

    //Assign target in the inspector.
    public Transform target;

    void Update() {
  
    //If you hit the tab button, this object will teleport to the target!
    if(Input.GetKeyDown(KeyCode.Tab))
         transform.position = new Vector3(target.transform.x , target.transform.y, target.transform.z);
    }
}

https://docs.unity3d.com/ScriptReference/Transform-position.html

Hope this helps!

oh it works now, thank you, thanks for your help

1 Like