Answers for "Moving an object back and forth on a single axis unity"

C#
2

Moving an object back and forth on a single axis unity

using UnityEngine;
using System.Collections;
 
public class MoveTest : MonoBehaviour
{
    public Vector3 pointB;
   
    IEnumerator Start()
    {
        var pointA = transform.position;
        while(true)
        {
            yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
            yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
        }
    }
   
    IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
    {
        var i= 0.0f;
        var rate= 1.0f/time;
        while(i < 1.0f)
        {
            i += Time.deltaTime * rate;
            thisTransform.position = Vector3.Lerp(startPos, endPos, i);
            yield return null;
        }
    }
}
Posted by: Guest on June-21-2020

Code answers related to "Moving an object back and forth on a single axis unity"

C# Answers by Framework

Browse Popular Code Answers by Language