C# script to make GameObject go between multiple 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

//Moves object between points

public class MoveBetweenPoints : MonoBehaviour {
    [SerializeField] private Transform objectToMove;
    [SerializeField] private bool moveThisObject = false;
    [SerializeField] private float moveSpeed;
    [SerializeField] private List<Transform> points;

    private int counter;
    private bool goBack;

    // Use this for initialization
    void Start()
    {
        counter = 0;
        goBack = false;

        if (moveThisObject)
        {
            objectToMove = transform;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (counter == (points.Count - 1))
        {
            goBack = true;
        }
        else if (counter == 0)
        {
            goBack = false;
        }


        if (objectToMove.position == points[counter].position)
        {
            if (!goBack)
            {
                counter++;
            }
            else
            {
                counter--;
            }
        }

        objectToMove.position = Vector3.MoveTowards(objectToMove.position, points[counter].position, moveSpeed * Time.deltaTime);
    }
}

Old Script:

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

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

public class MoveBetweenPoints : MonoBehaviour {
    public float moveSpeed;
    
    public List<Transform> points;

    private int counter;
    private bool goBack;
      
    // Use this for initialization
    void Start()
    {
        counter = 0;
        goBack = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (counter == (points.Count - 1))
        {
            goBack = true;
        } else if (counter == 0)
        {
            goBack = false;
        }


        if (transform.position == points[counter].position)
        {
            if (!goBack)
            {
                counter++;
            }
            else
            {
                counter--;
            }      
        }

        transform.position = Vector3.MoveTowards(transform.position, points[counter].position, moveSpeed * Time.deltaTime);
    }
}

 



Unity C# Move 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