Last active
September 2, 2023 22:44
-
-
Save lucasctnh/a4827c59c100a2ee1c9771f75241a752 to your computer and use it in GitHub Desktop.
Code Snippet - Infinite Move Left
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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