I want to move the object between the distance 0 and 100.
move it to the right when the distanceTravelled is get to 100 change direction.
When it’s getting to 0 again change direction again and so on.
And second option i want to make that when the distanceTravelled is got to 100 change direction and then when the object is getting to it’s originalPosition change direction again.
How can i make this both cases ? ( Each case alone but how to make them ? i mean same script but each time with the other option)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObject : MonoBehaviour {
public float moveSpeed = 2.0f;
private float distanceTravelled;
private Vector3 originalPosition;
// Use this for initialization
void Start ()
{
originalPosition = transform.position;
}
// Update is called once per frame
void Update()
{
distanceTravelled += Vector3.Distance(originalPosition, transform.position);
if (distanceTravelled < 100f)
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
if (distanceTravelled >= 100f)
{
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
}
}