Skip to content

Instantly share code, notes, and snippets.

@lucasctnh
Last active September 2, 2023 22:44
Show Gist options
  • Select an option

  • Save lucasctnh/a4827c59c100a2ee1c9771f75241a752 to your computer and use it in GitHub Desktop.

Select an option

Save lucasctnh/a4827c59c100a2ee1c9771f75241a752 to your computer and use it in GitHub Desktop.
Code Snippet - Infinite Move Left
public class MoveLeftAndRepeat : MonoBehaviour {
[Range(0f, 1f)]
[SerializeField] private float _customSpeed = 1f;
private Vector3 _startPos;
private float _repeatWidth;
private void Start() {
_startPos = transform.position;
_repeatWidth = GetComponent<BoxCollider>().size.x / 2 - GetComponent<BoxCollider>().center.x;
}
private void FixedUpdate() {
if (!GameManager.Instance.isGameRunning)
return;
transform.Translate(Vector3.left * GameManager.Instance.playerSpeed * _customSpeed * Time.fixedDeltaTime, Space.World);
if (transform.position.x < _startPos.x - _repeatWidth)
transform.position = _startPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment