Hey! I’m new to coding, and I need to make a cube that goes from left to right and so on forever in c# (all coded), but i cant figure out how to do it. I know how to move it on one direction, but cant make it stop at a point and go the other direction.
Thank you very much!! 
I was going to close this question as a duplicate since similar questions have been asked and answered in the last couple of years, but in a few minutes of Googling I did not find a good fit for an answer to point you to. So here is a new answer. Whenever you have something that is cycling back and forth, whether it is position, rotation, color, etc, consider using Mathf.Sin() or Mathf.PingPong(). Usually for movement of some sort, Mathf.Sin() is a better choice. Here is a sample script:
using UnityEngine;
using System.Collections;
public class BackAndForth : MonoBehaviour {
public float delta = 1.5f; // Amount to move left and right from the start point
public float speed = 2.0f;
private Vector3 startPos;
void Start () {
startPos = transform.position;
}
void Update () {
Vector3 v = startPos;
v.x += delta * Mathf.Sin (Time.time * speed);
transform.position = v;
}
}
int maxValue = 15; // or whatever you want the max value to be
int minValue = -15; // or whatever you want the min value to be
int currentValue = 0; // or wherever you want to start
int direction = 1;
Update() {
currentValue += Time.deltaTime * direction; // or however you are incrementing the position
if(currentValue >= maxValue) {
direction *= -1;
currentvalue = maxValue;
} else if (currentValue <= minValue){
direction *= -1;
currentvalue = minValue;
}
transform.position = new Vector3(currentValue, 0, 0);
}
Feel free to ask questions if any of this doesn’t make sense. It might need to be slightly altered for your specific implementation.
you can attach a rigibody to the cube and from code give it velocity to the left then you wait for a few seconds then give it velocity to the right and you put this in an infinite loop
How to apply rotation for object that goes left and right on fixed frequency ???
How to make an object stop at particular position.
Hello,I am trying to create an simple AR app.What I want to do is, just wanted my plane object to move left from the image target and stop.But what happening is,the plane object is continuously moving to the left position.
This is the code I have tried.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class vectors : MonoBehaviour {
public GameObject plane;
private Vector3 direction;
public static Vector3 left;
// Use this for initialization
void Start()
{
transform.position += Vector3.left * Time.deltaTime;
}
// Update is called once per frame
void Update()
{
transform.Translate(-1 * Time.deltaTime, 0, 0);
}
}
Can you please resolve my problem.
Thanks…
[186080-leftandright.txt|186080]
,using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
public Text text;
public bool moving = true;
private Vector2 newPos1;
private Vector2 newPos2;
void Start()
{
newPos1 = new Vector2(-5, 0);
newPos2 = new Vector2(5, 0);
}
void Update()
{
if (text.transform.position.x > newPos1.x && moving)
{
text.transform.position = Vector2.MoveTowards(text.transform.position, newPos1, 2 * Time.deltaTime);
}
else
{
moving = false;
text.transform.position = Vector2.MoveTowards(text.transform.position, newPos2, 2 * Time.deltaTime);
if (newPos2.x <= text.transform.position.x)
{
moving = true;
}
}
}
}