C# script to make GameObject teleport between points.

 

Content not available.
Please allow cookies by clicking Accept on the banner

Updated Script:

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

// © 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

//Teleports object between given points

public class TeleportBetweenPoints : MonoBehaviour {
    [SerializeField] private Transform objectToTeleport;
    [SerializeField] private bool teleportThisObject = false;

    [SerializeField] private float timeBetweenTeleports;
    [SerializeField] private List<Transform> points;

    private int counter;
    private bool goBack;
    private float timeBetweenTeleportsCount;

    private void Start()
    {
        counter = 0;
        goBack = false;

        timeBetweenTeleportsCount = timeBetweenTeleports;

        if (teleportThisObject)
        {
            objectToTeleport = transform;
        }
    }
  
    private void Update()
    {
        if (counter == (points.Count - 1))
        {
            goBack = true;
        }
        else if (counter == 0)
        {
            goBack = false;
        }

        if (timeBetweenTeleports <= 0.0f)
        {
            if (!goBack)
            {
                counter++;
            }
            else
            {
                counter--;
            }

            timeBetweenTeleports = timeBetweenTeleportsCount;
            objectToTeleport.position = points[counter].position;
        }

        timeBetweenTeleports -= Time.deltaTime;
    }
}

 

Old Script:

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

// © 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

public class TeleportBetweenPoints : MonoBehaviour {
    public float timeBetweenTeleports;

    public List<Transform> points;

    private int counter;
    private bool goBack;
    private float timeBetweenTeleportsCount;

    // Use this for initialization
    void Start () {
        counter = 0;
        goBack = false;
        timeBetweenTeleportsCount = timeBetweenTeleports;
    }
  
    // Update is called once per frame
    void Update () {
        if (counter == (points.Count - 1))
        {
            goBack = true;
        }
        else if (counter == 0)
        {
            goBack = false;
        }


        if (timeBetweenTeleports <= 0.0f)
        {
            if (!goBack)
            {
                counter++;
            }
            else
            {
                counter--;
            }

            timeBetweenTeleports = timeBetweenTeleportsCount;
            transform.position = points[counter].position;
        }


        timeBetweenTeleports -= Time.deltaTime;
    }
}

 

Unity C# Teleport GameObject Between Points
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close