C# script to move GameObject from point A to point B.

Updated Script:

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

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

//Moves object between two points

public class MoveBetweenTwoPoints : MonoBehaviour {
    [SerializeField] private float moveSpeed;

    [SerializeField] private GameObject pointA;
    [SerializeField] private GameObject pointB;

    [SerializeField] private bool reverseMove = false;
    [SerializeField] private Transform objectToUse;
    [SerializeField] private bool moveThisObject = false;

    private float startTime;
    private float journeyLength;
    private float distCovered;
    private float fracJourney;

    void Start()
    {
        startTime = Time.time;

        if (moveThisObject)
        {
            objectToUse = transform;
        }

        journeyLength = Vector3.Distance(pointA.transform.position, pointB.transform.position);
    }

    void Update()
    {
        distCovered = (Time.time - startTime) * moveSpeed;
        fracJourney = distCovered / journeyLength;

        if (reverseMove)
        {
            objectToUse.position = Vector3.Lerp(pointB.transform.position, pointA.transform.position, fracJourney);
        }
        else
        {
            objectToUse.position = Vector3.Lerp(pointA.transform.position, pointB.transform.position, fracJourney);
        }

        if ((Vector3.Distance(objectToUse.position, pointB.transform.position) == 0.0f || Vector3.Distance(objectToUse.position, pointA.transform.position) == 0.0f)) //Checks if the object has travelled to one of the points
        {
            if (reverseMove)
            {
                reverseMove = false;
            }
            else
            {
                reverseMove = true;
            }

            startTime = Time.time;
        }
    }
}

Old Script:

using UnityEngine;
using System.Collections;

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

public class MovingObject : MonoBehaviour {
    public float speed;
    public GameObject pointA;
    public GameObject pointB;
    public bool reverseMove = false;

    private float startTime;
    private float journeyLength;    

    // Use this for initialization
    void Start () {
        startTime = Time.time;
        journeyLength = Vector3.Distance(pointA.transform.position, pointB.transform.position);
    }
  
  // Update is called once per frame
  void Update () {
        float distCovered = (Time.time - startTime) * speed;
        float fracJourney = distCovered / journeyLength;

        if (reverseMove)
        {
            transform.position = Vector3.Lerp(pointB.transform.position, pointA.transform.position, fracJourney);
        }
        else
        {
            transform.position = Vector3.Lerp(pointA.transform.position, pointB.transform.position, fracJourney);
        }

        if ((Vector3.Distance(transform.position, pointB.transform.position) == 0.0f || Vector3.Distance(transform.position, pointA.transform.position) == 0.0f)) //Checks if the object has travelled to one of the points
        {
            if (reverseMove)
            {
                reverseMove = false;
            }
            else
            {
                reverseMove = true;
            }

            startTime = Time.time;
        }
    } 
}

 

Unity C# Moving GameObject from point A to point B
Tagged on:             

2 thoughts on “Unity C# Moving GameObject from point A to point B

  • August 2, 2016 at 1:01 pm
    Permalink

    tnx it works perfectly

    Reply
    • August 2, 2016 at 1:12 pm
      Permalink

      I’m glad to hear that 🙂

      Reply

Leave a Reply to TheFlyingKeyboard Cancel reply

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