Search Unity

Question how to move npc back and forth between boxes

Discussion in '2D' started by Deleted User, Jun 23, 2021.

  1. Deleted User

    Deleted User

    Guest

    I am trying to move my npc between boxes rather then going through it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Patrol : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.     public float distance;
    10.     private bool movingRight = true;
    11.     public Transform groundDetection;
    12.  
    13.     void Update()
    14.     {
    15.  
    16.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    17.  
    18.         RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2.0f, LayerMask.GetMask("Ground"));
    19.  
    20.         if (groundInfo.collider == false)
    21.         {
    22.             if (movingRight == true)
    23.             {
    24.                 transform.eulerAngles = new Vector3(0, -180, 0);
    25.                 movingRight = false;
    26.             }
    27.             else
    28.             {
    29.                 transform.eulerAngles = new Vector3(0, 0, 0);
    30.                 movingRight = true;
    31.             }
    32.         }
    33.  
    34.     }
    35.    
    36.  
    37. }
    ezgif.com-gif-maker.gif
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Try one of these videos: upload_2021-6-23_13-35-37.png