Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Collider interferes with script.

Discussion in '2D' started by Retro420, Nov 4, 2018.

  1. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    18
    Hello.
    I am still new to coding, happily class myself as a beginner, and am currently using Unity 5.5(my favourite ).

    I am in the middle of a 2d project and have just finished the animation. Animation went better than I could have hoped but I can’t add a collider. If I don’t have a collider attached everything works perfectly, right animation at right time etc etc, except obviously my player won’t collide with anything. If I attach a collider then the player goes straight to the death animation and falls through the floor into oblivion.

    I was originally going to use the polygon 2d collider but also tried box, capsule and mesh to no avail.

    The player is a 2d sprite with sprite renderer, rigidbody 2d, animator and my player script attached.

    Any advice would be great.
    Thank you.
     
  2. Tim-Duval

    Tim-Duval

    Joined:
    Sep 2, 2015
    Posts:
    193
    Have you tried adjusting the scale of the collider? If it even remotely touches the ground, your player will fall through. so sometimes you need your capsule collider (or whichever collider component you're using) to be above the feet of the player. You can also move your player up and let gravity bring the player back down to the surface.

    If you elect to go without a collider, you can insert a raycast from the feet of the player and determine the distance from the collider its reading. In this instance it would be the ground, so your ground tiles would need to be tagged accordingly.
    You could then create a racast in front & behind the player, like a forcefield and if anything penetrates that forcefield, you could work collisions and/or damage in that fashion.
     
    Retro420 likes this.
  3. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    18
    Hi. Thank you for the reply.
    I have tried adjusting the collider and trying different ones, but it doesn’t help. I actually always let my player drop a bit to get a “true” y starting position but that hasn’t helped. For some reason my player skips everything and goes straight to the “dead” animation and the script stops so the player just falls through everything.

    I am not very knowledgeable with raycasting so i will look at some tutorials later, just feels like colliders would be easier as it is a very simple game anyway, they just aren’t working for me lol.

    Thank you again for the reply.
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    870
    Post the scrpt (player script ?) where are the conditions to start the death animation.
    It is just hard to say what is wrong without to see anything.
     
  5. AnneSchmidt

    AnneSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    901
    vakabaka likes this.
  6. Tim-Duval

    Tim-Duval

    Joined:
    Sep 2, 2015
    Posts:
    193
    The only thing that I can think of is that you're using a 3D collider with a 2D collider or vice-versa. If your terrain is 2D and your player is 3D, put a 3D collider on the terrain as well. ( Or vice-versa )
     
    Retro420 likes this.
  7. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    18
    Sorry for the delay.
    Here is my player script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     public float upForce;                  
    8.     private bool isDead = false;          
    9.  
    10.     private Animator anim;                  
    11.     private Rigidbody2D rb2d;              
    12.  
    13.     void Start()
    14.     {
    15.        
    16.         anim = GetComponent<Animator> ();
    17.  
    18.         rb2d = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.        
    24.         if (isDead == false)
    25.         {
    26.            
    27.             if (Input.GetMouseButtonDown(0))
    28.             {
    29.                
    30.                 anim.SetTrigger("Jump");
    31.  
    32.                 rb2d.velocity = Vector2.zero;
    33.  
    34.                 rb2d.AddForce(new Vector2(0, upForce));
    35.             }
    36.         }
    37.     }
    38.  
    39.     void OnCollisionEnter2D(Collision2D other)
    40.     {
    41.        
    42.         rb2d.velocity = Vector2.zero;
    43.  
    44.         isDead = true;
    45.  
    46.         anim.SetTrigger ("Die");
    47.  
    48.         GameController.instance.PlayerDied ();
    49.     }
    50. }
    51.  
    Everything in the scene is a 2d sprite, there is only collideron my player sprite at the moment.
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    870
    add if statement to the collisions event. And check which one object (you can use a tag for check) is colliding with the player.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2.     {
    3.         if (other.gameObject.CompareTag("BombAsExample")) {
    4.         rb2d.velocity = Vector2.zero;
    5.         isDead = true;
    6.         anim.SetTrigger ("Die");
    7.         GameController.instance.PlayerDied ();
    8.     }
    9. }
     
  9. Retro420

    Retro420

    Joined:
    Sep 5, 2016
    Posts:
    18
    Wow! Thank you so much.

    I cant believe that fixed it. When it wasnt working I didnt have any colliders on the screen, and my player would just collapse in the die animation and not do anything.

    Its always the simple bits that I get stuck on. Thank you.
    And thank you to everyone who answered. :)