i want to move a game object from point A to point B repeatedly ( it should appear again at point A without going back to point A )how do i script this?
1 Answer
1Create GameObject which will be the one that moves from point A to point B. Drop the script on that GameObject. Next, create two GameObjects. Those will represent the points. Set their positions and assign them to the “Point A” and “Point B” variables in the script in the inspector. Finally, set the speed to whatever value you want it to be. Here’s the script:
[SerializeField]
private Transform pointA;
[SerializeField]
private Transform pointB;
[SerializeField]
private float speed;
private void Awake()
{
transform.position = pointA.position;
}
private void Update()
{
if (transform.position==pointB.position)
transform.position = pointA.position;
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, pointB.position, step);
}
thank you it works perfectly
– g_v_d@piotrek_81 when the object moves to point B ,I want it to reach point A after 5 seconds how should I do this one?really appreciate the help
– g_v_d