Search Unity

Have Two Instantiated Prefab Clones That Collide, Want Only One To Stop Translating Its Transform

Discussion in '2D' started by randumzgaming, Nov 27, 2022.

  1. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    Making a 2D project involving cars that drive on a straight and 90 degree angle path. I have a spawn point Instantiating clones of a prefab car. After they spawn they have a movement script that translates their transform with a bool flag that determines moving forward verse being stopped.

    The cars do a great job stopping when they collide but I'm having a terrible time trying to figure out how to get the cars moving again :(

    I tried setting a random timer on a coroutine to trigger the drive bool which was great when the first prefab got the lower number but when the second one was lower it would run over the first one looking bad.

    My paradox is that any code I throw at it effects both objects so I cant get one to start moving without the other hitting it again.
     
  2. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    210
    Using the translate function to override the transform on a gameObject might not be the best implantation here, that's because it totally disregards the use of the physics system, therefore you'll get no collision detection etc. I'd recommend firstly re-writing the logic so that it's using Unity's physics system, so applying RigidBody's and Colliders to the GameObjects (in this case a car) and then moving the cars by applying force or altering the velocity directly on the RigidBody tied to the GameObject. Give this video a watch, it explains the different ways to move GameObjects quite nicely:

     
  3. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    I'm making a 2D top down game so I wont be using the Physics system at all. Its all turned off.

    Thank you for the response but I actually found the answer I was looking for here, specifically the response from nickhoude21. https://forum.unity.com/threads/checking-if-a-specific-collider-is-colliding.396503/

    By adding a polygon collider to the back and sides of the car I'm able to detect a collision from the the rear and sides of the vehicle allowing the second car to put its "brakes" on without the first one also doing the same.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    Which includes colliders which you're saying you're using. Physics is not just a Rigidbody2D. That's a contradiction. You cannot simply modify the Transform when using 2D physics. It'll give you all sorts of problems and the worst performance. A collider without a Rigidbody2D is Static (non-moving). Modifying the Transform will cause it to be completely recreated.
     
  5. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    Are you saying that if I translate the transform of a gameobject (object has colliders but no rigidbody2D) using FixedUpdate, Unity is recreating the colliders every frame, wasting resources?
     
  6. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    Thank you for this video! I will look more into using velocity to move these cars around.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    Yes, you are wasting resources doing stuff the absolute wrong way. Transform changes are not movement, they are instant teleporting. You should always use the Rigidbody2D API. In the last 9 years I've been saying this, it amazes me how often devs think that not adding a Rigidbody2D (to make it Static) and then repositioning it is a good thing!

    Colliders are attached to Rigidbody2D and move with them with very little effort.

    There's a body-type on a Rigidbody2D for a reason. If you want to explicitly move a collider then attach it to a Rigidbody2D and set its body-type to Kinematic then use its API to cause movement. Hey presto, no collider actions required.

    In short, Rigidbody2D move, colliders don't.
     
  8. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    Cancel that velocity comment, MovePosition seems more appropriate with what I'm trying to accomplish.
     
  9. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    Thank you for the clarity. I've watched the video NeilB133 posted and I would like to hear your thoughts on the pros and cons of using velocity vs movePosition.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    There are not pros and cons. They are different things with different purposes. Learn what each does, choose the most appropriate for what you do.
     
    NeilB133 likes this.
  11. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    So I have switched my game to use physics and for the most part everything already in place still works. New problem with adding rigidbodies is the cars no longer wait in line if traffic builds up and just start pushing each other around.

    I tried freezing the X and Y positions on collision which stops them pushing but I cannot unfreeze them using onCollisionExit as the collisions never exit.

    Thoughts on how I can prevent these instantiated prefab clones from pushing each other, stop movement when they collide, then resume movement once the player has moved out of the way from the first car?
     
  12. randumzgaming

    randumzgaming

    Joined:
    Nov 27, 2022
    Posts:
    12
    Nevermind I figured it out myself :)

    What a nightmare converting from transform.translate to using rigidbody and physics and it took all morning but I did it. Thanks.
     
    MelvMay likes this.