Search Unity

My collision detection / death script is not running right

Discussion in 'Scripting' started by Lilspookles, Oct 13, 2019.

  1. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    Hey guys so basically I've been following a youtube tutorial while making my first game. I have tried using the same script he was using and I also tried the fixes I have seen in the comments. What I am doing is making it to where when a player collides with an object tagged as 'Obstacle' then the movement stops. But my problem is that despite my if statement narrowing it to only those tagged with 'Obstacle', it still reads the ground as an 'Obstacle'. the ground has no tags and the obstacle one is the only object tagged with it

    code: ( I am using C# (Csharp))


    Code (Csharp):
    1. using UnityEngine;
    2.  
    3. public class playercollision : MonoBehaviour
    4. {
    5.  
    6.     public PlayerMovement movement;
    7.  
    8.  
    9.     void OnCollisionEnter (Collision collisionInfo)
    10.     {
    11.         if (collisionInfo.collider.tag == "Obstacle");
    12.         {
    13.             movement.enabled = false;
    14.         }
    15.     }    // NOT WORKING> DETECTING GROUND AS "Obstacle"
    16.  
    17. }
    18.  
     
    Last edited: Oct 13, 2019
  2. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    I have also tried a couple different ways of rewriting it to more recent updates and that hasnt fixed it
     
  3. Please use CODE tags. Code isn't readable as text: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Good, because you cannot use anything else, lately.

    First and most important: do you have any errors in the console? If yes, what are those?

    What happens, if you put the line Debug.Log(collisionInfo.collider.name); above the movement.enabled = false;?
    Do your collision happen? If not, check if your tags assigned properly and both gameobjects have proper colliders.
     
  4. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    the collision happens but the movement is stopping when it hits the ground. it is only supposed to stop when the player hits the obstacle. i get a readback from console saying it had hit Ground
    There are no errors at all
     
  5. Do you have your "ground" game object(s) tagged as "Obstacle"?
    Do you have other OnCollision methods regarding the ground?
     
  6. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    is your game 2d or 3d?
    Can you move at all?
    and send youtube link
     
    Last edited: Oct 13, 2019
  7. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    No I don't to both questions
     
  8. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    3D
    Yes

     
  9. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
  10. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    thanks : - ) and tutorial link? or just post in code tags whats inside PlayerMovement.cs
     
  11. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    Code (Csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.     public float forwardForce = 2000f;
    8.     public float sidewaysForce = 500f;
    9. void Start()
    10.     {
    11.     }
    12.     // Fixed Update for physics
    13.     void FixedUpdate()
    14.     {
    15.         rb.AddForce(0, 0, forwardForce * Time.deltaTime); // time delta is for consistancy across framerates
    16.         if (Input.GetKey("d"))
    17.         {
    18.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    19.         }
    20.         if (Input.GetKey("a"))
    21.         {
    22.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    23.         }
    24.     }
    25. }
    26.  
     
  12. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    and if you remove slippery material from ground?
     
  13. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    Same thing, the ground is detected as an Obstacle and the player stops moving
     
  14. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    send please the tutorial link : - )
     
  15. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
     
  16. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    and this?
    Code (CSharp):
    1. void OnCollisionEnter (Collision collisionInfo)
    2.     {
    3.         if (collisionInfo.gameObject.tag == "Obstacle");
    4.         {
    5.             movement.enabled = false;
    6.         }
    7.     }
    if nothing,
    create Obstacle script, put it onto the obstacle and
    instead of checking tag do this:
    Code (CSharp):
    1.     void OnCollisionEnter (Collision collisionInfo)
    2.     {
    3.         if (collisionInfo.gameObject.GetComponent<Obstacle>() != null);
    4.         {
    5.             movement.enabled = false;
    6.         }
    7.     }
     
    Last edited: Oct 13, 2019
  17. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14

    no, none of it worked
    I dont know whats happening honestly
     
  18. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    with the obstacle component check it stills counts as obstcle?
     
  19. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    Yeah im so confused. the ground is still being counted as an obstacle when it has nothing to do with the obstacle thing
     
  20. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    Still no viable fix, please help
     
  21. Zip your assets folder and upload it, I will take a look later what's happening.

    Also an advice to the future: it is a good habit to follow the tutorials in the same version of Unity what is used in it. So you don't get lost and you don't misunderstand something (I'm not sure if this is the case, but I guess).
     
  22. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
     

    Attached Files:

  23. Delete the ; from the line
    Code (CSharp):
    1. if (collisionInfo.collider.tag == "Obstacle");
     
    Reedex likes this.
  24. Lilspookles

    Lilspookles

    Joined:
    Jun 6, 2019
    Posts:
    14
    I feel dumb now lol.. If you dont mind explaining why you dont need it?
     
  25. Because ; means you close a statement. if(something) doSomething(); is a statement. If >something< is true, execute doSomething(). The empty ; means empty statement, so if (something) ; means if something is true, do nothing and move on.
    After this, you have the {...} block, which will always be executed, since the if(somehting) only controls the empty statement.
    This is why you had your bug.
     
  26. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    don't feel dumb, i overlooked that too,..
     
    CelticWarriorMoon likes this.
  27. TheUnityUser17

    TheUnityUser17

    Joined:
    Jan 21, 2024
    Posts:
    1
    Well, looking through this thread reminds me that even the biggest bugs in other projects can be solved in simplest solutions.