Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Chibi : MonoBehaviour
- {
- [SerializeField] private float speed = 2f;
- [SerializeField]
- private LayerMask Wall;
- private Rigidbody2D myRigidbody;
- private bool isStopped = false;
- private float[] stopTimes = new float[] { 1f, 3f, 6f, 9f };
- private void Start()
- {
- myRigidbody = GetComponent<Rigidbody2D>();
- StartCoroutine(StopRoutine());
- }
- private void Update()
- {
- if (!isStopped)
- {
- Move();
- }
- else
- {
- myRigidbody.linearVelocity = Vector2.zero;
- }
- }
- private void Move()
- {
- myRigidbody.linearVelocity = new Vector2(transform.localScale.x * speed, myRigidbody.linearVelocity.y);
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (((1 << collision.gameObject.layer) & wallLayer) != 0)
- {
- TurnAround();
- }
- }
- private void TurnAround()
- {
- Vector3 scale = transform.localScale;
- scale.x *= -1;
- transform.localScale = scale;
- }
- private IEnumerator StopRoutine()
- {
- while (true)
- {
- yield return new WaitForSeconds(Random.Range(2f, 5f));
- isStopped = true;
- float stopTime = stopTimes[Random.Range(0, stopTimes.Length)];
- yield return new WaitForSeconds(stopTime);
- isStopped = false;
- }
- }
- }
Advertisement