Search Unity

Automated Movement And Collisions

Discussion in 'Scripting' started by masterpug13, Apr 12, 2019.

  1. masterpug13

    masterpug13

    Joined:
    Mar 31, 2019
    Posts:
    19
    Greetings! Total c# noob.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class GreenWayfinder : MonoBehaviour
    7. {
    8.     public float speed;
    9.     private Rigidbody2D greenHero;
    10.  
    11.     public bool isWalking;
    12.     public float walkTime;
    13.     private float walkCounter;
    14.     public float waitTime;
    15.     private float waitCounter;
    16.     public int walkDirection;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         greenHero = GetComponent<Rigidbody2D>();
    22.         waitCounter = waitTime;
    23.         walkCounter = walkTime;
    24.  
    25.         ChooseDirection();
    26.     }
    27.  
    28.  
    29.     void Update()
    30.     {
    31.                
    32.         //Movement
    33.         if (isWalking)
    34.         {
    35.             walkCounter -= Time.deltaTime;
    36.  
    37.             switch (walkDirection)
    38.             {
    39.                 case 0:
    40.                     greenHero.velocity = new Vector2(0, speed);
    41.                     break;
    42.  
    43.                 case 1:
    44.                     greenHero.velocity = new Vector2(0, -speed);
    45.                     break;
    46.  
    47.                 case 2:
    48.                     greenHero.velocity = new Vector2(speed, 0);
    49.                     break;
    50.  
    51.                 case 3:
    52.                     greenHero.velocity = new Vector2(-speed, 0);
    53.                     break;
    54.             }
    55.        
    56.  
    57.             if (walkCounter <= 0)
    58.             {
    59.                 isWalking = false;
    60.                 waitCounter = waitTime;
    61.             }
    62.         }
    63.         else
    64.         {
    65.             waitCounter -= Time.deltaTime;
    66.  
    67.             greenHero.velocity = Vector2.zero;
    68.  
    69.             if (waitCounter < 0)
    70.             {
    71.                 ChooseDirection();
    72.             }
    73.         }
    74.     }
    75.  
    76.     //Colision detection and functionality
    77.     public void OnControllerColliderHit(ControllerColliderHit hit)
    78.     {
    79.         switch (hit.gameObject.tag)
    80.         {
    81.             case "Door":
    82.                 transform.position = hit.transform.GetChild(0).position;
    83.                 break;
    84.         }
    85.     }
    86.  
    87.     //Random direction selection
    88.     public void ChooseDirection()
    89.     {
    90.         walkDirection = Random.Range (0, 4);
    91.         isWalking = true;
    92.         walkCounter = walkTime;
    93.     }
    94.  
    95.  
    96. }
    97.  
    I set up a script that causes the enemy character (greenHero) to move in random directions. The issue I am having is that when the enemy collides with a door, I want to teleport them to the other side. They tend to get stuck in hallways otherwise.

    The issue I am having is that I cannot get the character to actually teleport. It seems like it is only obeying the random movement, and ignoring what it should do when hitting the door colider. Seems like I need to do some kind of check in the movement part, but I am not sure exactly how to do that.
     
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    You may have to normalize the position. "hit.transform.GetChild(0).position" might be relative to the hit, not the global space.

    Barring that, you should just debug. Does OnControllerColliderHit get called? Is the tag really "Door?" If so, what's the position before and after the change?
     
  3. masterpug13

    masterpug13

    Joined:
    Mar 31, 2019
    Posts:
    19
    Thanks for the reply!
    I was able to fix this with some additional noodling. It looks like I had to use OnCollision2D since the door is using a 2D colider. Doing that (and doing some other tweaks), it looks like it works now.