Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Collision2D not working as expected for movement

Discussion in '2D' started by bwool, Aug 2, 2023.

  1. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    20
    Apologies if this is a dumb question, but this makes no sense to me.

    I'm trying to create a basic unit controller for a 2D RTS style game. Currently, I am trying to add the simple movement mechanics of automatically moving to the right unless the unit makes contact with another object tagged "enemy" or "end".

    You can see in the code below, that the script is extremely simple, and the the scene only consists of the unit with attached movement script, RigidBody2D, and BoxCollider2D; the enemy with attached RigidBody2D and BoxCollider2D; and the end which only has a BoxCollider2D(isTrigger).

    The unit will move to the right and then correctly stop for the end gameobject, but the enemy gameobject does not stop the unit, and is instead pushed back by the same velocity indefinitely.

    Running the Debugs, I've noticed that the collision with the enemy is being detected, but for some reason the MovementBool change is not being acknowledged by the Movement() function, causing the unit to continue to move to the right.

    This is very confusing to me as the exact same output from detecting the end rather than the enemy collision will successfully change the bool and stop movement.

    Code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6. using TMPro;
    7.  
    8. public class BasicCombatController : MonoBehaviour
    9. {
    10.     public Rigidbody2D unitrb;
    11.     bool MovementBool = true;
    12.  
    13.     private void Start()
    14.     {
    15.         Debug.Log("Void Start");
    16.         unitrb = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         Movement();
    22.     }
    23.  
    24.     // Move right at 2 velocity if bool is true
    25.     private void Movement()
    26.     {
    27.         if (MovementBool == true)
    28.         {
    29.             Debug.Log("Movement");
    30.             unitrb.velocity = new Vector3(2, 0, 0);
    31.         }
    32.     }
    33.  
    34.     // Stop movement when end or enemy collider enters
    35.     private void OnCollisionEnter2D(Collision2D other)
    36.     {
    37.        
    38.         if (other.gameObject.CompareTag("end"))
    39.         {
    40.             Debug.Log("Stop for End");
    41.             MovementBool = false;
    42.         }
    43.         if (other.gameObject.CompareTag("enemy"))
    44.         {
    45.             Debug.Log("Stop for Enemy (enter)");
    46.             MovementBool = false;
    47.         }
    48.     }
    49. }
    50.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You're setting booleans to false... that's nice.

    Where are you setting velocity to zero?
     
    bwool likes this.
  3. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    20
    Good question. I wasn't!

    It works now, and I mirrored it onto another script for the enemy. Now they walk towards each other, and stop on contact as desired.

    Thank you!
     
    Kurt-Dekker likes this.