Advertisement
OwlyOwl

sample_platform_patrol

May 17th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AiPatrol : MonoBehaviour
  6. {
  7.     [SerializeField] float _speed;
  8.     [SerializeField] float _distance;
  9.     [SerializeField] Transform groundDetection;
  10.     private bool _movingRight = true;
  11.  
  12.     private void Update()
  13.     {
  14.         transform.Translate(Vector2.right * _speed * Time.deltaTime);
  15.  
  16.         RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, _distance);
  17.         if (groundInfo.collider == false)
  18.         {
  19.             if (_movingRight == true)
  20.             {
  21.                 transform.eulerAngles = new Vector3(0, -180, 0);
  22.                 _movingRight = false;
  23.             }
  24.             else
  25.             {
  26.                 transform.eulerAngles = new Vector3(0, 0, 0);
  27.                 _movingRight = true;
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement