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

Question Collision not detected

Discussion in 'Scripting' started by DJF12345, Nov 15, 2022.

  1. DJF12345

    DJF12345

    Joined:
    Oct 4, 2022
    Posts:
    6
    Hi, I have a basic scene with two primitives, the Player (sphere) and a structure (cube). They both have colliders and rigidbodys.

    I have a debug log in the OnCollisionEnter function, but when the player hits the structure I don't get anything in the log. I'm a relative beginner, but I've done a few tutorials so I know a little more than nothing. :)

    Any help?

    Here is the Player's inspector:



    And the structure's inspector:



    In the PlayerController.cs script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed = 10.0f;
    8.     private float horizontalInput;
    9.     private float forwardInput;
    10.     public Rigidbody rb;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void FixedUpdate()
    20.     {
    21.         // See Project Settings > Input Manager
    22.         horizontalInput = Input.GetAxis("Horizontal");
    23.         forwardInput = Input.GetAxis("Vertical");
    24.  
    25.         // So Player will move forward at X m/s
    26.         transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
    27.         transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
    28.  
    29.         if (transform.position.y != 0.5f)
    30.         {
    31.             transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z);
    32.         }
    33.  
    34.     }
    35.  
    36.     void OnCollisionEnter(Collision collision)
    37.     {
    38.         if (gameObject.CompareTag("Structure"))
    39.         {
    40.             Debug.Log("Structure detected");
    41.             rb.velocity = Vector3.zero;
    42.         }
    43.     }
    44. }
    45.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Because your Debug.Log is inside your check, you don't know if it's being called or not. You should also put such a thing outside of any check if you are saying it's not being called back.

    Code (CSharp):
    1.  void OnCollisionEnter(Collision collision)
    2. {
    3.    Debug.Log("OnCollisionEnter Called. I was wrong!");
    4.    ...
    5. }
    Look at your check. You're saying compare the GameObject the script is on with the tag "Structure". This is nothing to do with the incoming collision. You should use "if (collision.gameObject.CompareTag("Structure"))".

    Also, there'a absolutely no point in resetting the velocity to zero, it's not moving via its velocity. What you are doing however is something you should never do which is change the Transform. The Rigidbody is responsible for writing to the Transform, your responsibility is to use the Rigidbody API to cause it to move i.e. add forces etc. Don't change the Transform.
     
  3. FrankMedia

    FrankMedia

    Joined:
    May 27, 2013
    Posts:
    4
    Hi

    You have to put the (collision) parameter of the OncollisionEnter function in the if

    Code (CSharp):
    1. if (collision.gameObject.CompareTag("Structure"))
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
    Olipool likes this.
  5. DJF12345

    DJF12345

    Joined:
    Oct 4, 2022
    Posts:
    6
    Lots of useful replies there, thank you. I've learned not to manipulate the transform to say the least!

    I'll try fix my code and let you know how it goes.

    Thanks again.
     
    Kurt-Dekker and MelvMay like this.
  6. DJF12345

    DJF12345

    Joined:
    Oct 4, 2022
    Posts:
    6
    Just wanted to confirm that it worked. Thanks again.
     
    Kurt-Dekker and MelvMay like this.