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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Collision Question - Part2.

Discussion in '2D' started by CelticKnight, Jul 22, 2015.

  1. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Hello guys,

    I have a problem which is driving me nuts! I have tried and tried and tried to figure out what is wrong and I'll be buggered if I can find the problem.

    The problem is this - I need to know if a missile, bullet, projectile whatever you wish to call it, is hitting a platform object, so, I can stop it progress, have an explosion prefab occur and destroy the projectile game object. But I cannot get it to register.

    The platforms all have the rigidbody2d attached to them, and box colliders, and OnCollisionEnter2D functions as do the clone projectiles. But I don't know if anything I have done is correct. It is part of a Brackeys tutorial which isn't the clearest guide and with elements that leave me baffled, but, it's all I've got. And this is part of my attempt to clear up some of that up.

    So, if you are willing to help what do I need to supply to get an answer? I'll supply whatever is necessary to get to the root of this problem.

    Thankyou and Regards,
    CK.

    edit: After doing a search on the properties of the RigidBody2D I found that setting the Is Kinematic to true stops it object reacting to collisions - is this true? Without being able to set it to true the platform falls of the bottom of the screen :confused: How can I set it to react to bullet collisions if that is the case?
     
    Last edited: Jul 22, 2015
  2. Zoltern

    Zoltern

    Joined:
    Jun 16, 2015
    Posts:
    14
    For the platforms, freeze the Y position.

    For the bullet, tag or layer your platforms, and check for them in the collision method (onCollisionEnter2D). From that, set a parameter for the animator to play the explosion (if you want explosions on the wall) and then, on the last frame, add an event. That event will map a method of the bullet script that will just destroy itself :)
     
  3. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    Also, if your platforms will never need to move, you don't need a Rigidbody2D attached to them. You'd only need that if you needed physics to act on them in some way.

    I don't think making the rigidbody2D kinematic will prevent it from reacting to collisions.

    If you're still not seeing the collision then posting your code and the properties of the relevant objects would be helpful.
     
  4. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    As soon as a saw your reply, I thought, of course, that is the answer, I am familiar with the constraints from 3D and used them in the past, it made perfect sense - so I immediately went to my project and went to set that property and well in my Rigidbody 2D box that property doesn't exist :confused:. It shows it as clear as day in the docs at: http://docs.unity3d.com/Manual/class-Rigidbody2D.html

    So how hold I go about setting about freezing the Y position? Is there a coding solution perhaps?

    Like I said I am learning about 2D from the Brackeys tutorial and some of his ideas and concepts are ones that I never would've used, for example a class hard coded inside a class, I am used to the GameMaker way of doing things, so, this way of using Rigidbodies, kinematics, using addforce and those concepts are completely foreign to me. I have seen and used some of Unity's tutorials in the past and have no desire to go down that path again.

    I just want to learn some 2D concepts in Unity and get it working! But this bullet thing has me beat at the moment - the worst part is I have *done* this in 3D with less fuss than I can in 2D :p.
     
    Last edited: Jul 23, 2015
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you tried a break point to debug & check that the collision is registering?

    Your code for the collision will help, make sure you use the code tags so it is displayed correctly

    Also, check they they are on the same z coordinates just in case that impacts it
     
  6. JustinTheDev

    JustinTheDev

    Joined:
    Jul 23, 2015
    Posts:
    3
    Is you check for collisions on the environment piece or on the projectile?
     
  7. Leo-Yaik

    Leo-Yaik

    Unity Technologies

    Joined:
    Aug 13, 2014
    Posts:
    434
    Are you using box colliders or box collider 2d?
     
  8. astromedia-only

    astromedia-only

    Joined:
    May 13, 2014
    Posts:
    25
    Maybe im wrong but if you dont want platform to be falling maybe will help setting gravity option (there was something like that) to zero? Than you dont need that weird "kinematic" option, so turn it off and it should be ok, am i wrong? :)
     
  9. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Sorry for the taking so long to respond I have been busy the martial arts workshops and preparing for a demonstration. Now after all your replies and taking a good hard look at the documentation and trying to understand exactly what it is that the "Is Kinematic" option does - I never studied physics - and "eventually" found out why the collisions weren't registering. It was because of the missiles being set to "Is Kinematic" - doh!

    Though that had a curious effect of having the bullet follow the the top of the platforms (set as "Is Kinematic",so, the platforms wouldn't fall of the screen), until they finished before resuming movement on the Y - Axis. So, I need to figure out what I am going to do with the bullets, let them pass through the platforms unimpeded or have them explode (destroy game object) as soon as they hit. It's especially bad when shooting directly below the player as he goes up further and further in the air until the objects are destroyed, and the sprite ends up goodness knows where. I think the first option is the most reasonable as the player would still go into the air with the explosion prefab(s) underneath it.

    Thanks again for all your replies.