#Hello I have a weird bug with my game I
1 messages · Page 1 of 1 (latest)
the code that I use :
public class MovingPlatform : MonoBehaviour
{
public List<Vector2> positions;
public float movementSpeed;
private int movementStep = 0;
private bool reverse;
public enum MovementType
{
PING_PONG,
NORMAL
};
public MovementType movementType;
public bool debug;
void Start()
{
if(positions.Count > 0)
{
transform.position = positions[0];
}
}
// Update is called once per frame
void Update()
{
if (positions.Count < 2) return;
if (Vector2.Distance(transform.position, positions[movementStep]) > 0.01F)
{
Vector2 dir = (positions[movementStep] - (Vector2)transform.position).normalized;
transform.Translate(dir * Time.deltaTime * movementSpeed);
if (debug) Debug.Log(dir + " " + Time.deltaTime + " " + movementSpeed);
}
else
{
if(movementType == MovementType.NORMAL)
{
movementStep = (movementStep + 1) % positions.Count;
}
else if(movementType == MovementType.PING_PONG)
{
if (!reverse)
{
movementStep++;
if (movementStep == positions.Count - 1) reverse = true;
}
else
{
movementStep--;
if(movementStep == 0) reverse = false;
}
}
}
if (debug) Debug.Log(movementStep + " " + gameObject.name + " " + transform.position);
}
}
@scenic breach the code is here if you want
hey, did you manage to get this working?
you should take a look at the conditions. Try adding some logs to see if there's any difference in the conditions results for build and editor
Yes it was a precision issue in this condition
if (Vector2.Distance(transform.position, positions[movementStep]) > 0.01F)
I replaced it with
if (Vector2.SqrMagnitude((Vector2)transform.position - positions[movementStep]) > 0.1F*0.1F)
You could try using https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html
It may be more precise
Oh great, I will use it thanks