C# script to make GameObject teleport between points.

 

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 *

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
504