So i got a Scripts which checks the start and end position of moving Platforms. Now i want those platforms to turn around when they hit the end position, but dont really now how to check at what time that state is reached because I dont know yet if the x, y or z coordinates are higher or lower compared to the startPos.
Does anyone have an idea how i could handle this or how i have to think about this? Im quite new to c# and unity…
Here is the Script, I first tried to check when pos.x>=startPoint.x+=dis.x and so one, but i never know if it will be <= or >=…
using UnityEngine;
using System.Collections;
public class MovePlatform : MonoBehaviour {
public Vector3 startPoint;
public Vector3 endPoint;
public float speed = 1;
private Vector3 pos;
private Vector3 dis;
private Vector3 moveV;
private bool turned=false;
private float distance;
void Start () {
pos = startPoint;
this.transform.position = pos;
dis = endPoint - startPoint;
distance = Mathf.Sqrt((dis.x * dis.x) + (dis.y * dis.y) + (dis.z * dis.z));
moveV.x = (dis.x / distance) * speed * Time.deltaTime;
moveV.y = (dis.y / distance) * speed * Time.deltaTime;
moveV.z = (dis.z / distance) * speed * Time.deltaTime;
}
void Update () {
pos = this.transform.position;
if (turned == false) pos += moveV;
else if (turned == true) pos -= moveV;
this.transform.position = pos;
}
}