Search Unity

Collider not being detected

Discussion in 'Scripting' started by DeadlyPlayGames, Dec 11, 2018.

  1. DeadlyPlayGames

    DeadlyPlayGames

    Joined:
    Dec 11, 2018
    Posts:
    1
    Hello all,

    I have an issue where the box collider is not being detected when the player collider. I'm trying to make the player stop when the player collides with the 'Obstacle'. Some help would be greatly appreciated.

    Many thanks.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerCollision : MonoBehaviour
    4. {
    5.     public PlayerMovement movement;
    6.  
    7.     void OnCollisionEnter(Collision obj)
    8.     {
    9.         if (obj.transform.tag == "Obstacle")
    10.         {
    11.             movement.enabled = false;
    12.         }
    13.  
    14.     }
    15.  
    16. }
    17.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovement : MonoBehaviour {
    4.     public Rigidbody rb;
    5.     public float forwardForce = 2000f;
    6.     public float sidewaysForce = 300f;
    7.     public float upForce = 150f;
    8.    
    9.     // FixedUpdate because we're using physics
    10.     void FixedUpdate () {
    11.         rb.AddForce(0, 0, forwardForce * Time.deltaTime); //force of 2000 units on Z-axis, deltaTime balances out the fps
    12.         if (Input.GetKey("d") )
    13.         {
    14.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
    15.         }
    16.  
    17.         if (Input.GetKey("a") )
    18.         {
    19.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
    20.         }
    21.         if(Input.GetKey("w") )
    22.         {
    23.             rb.AddForce(0, upForce, 0);
    24.         }
    25.     }
    26. }
     
  2. Tockster111

    Tockster111

    Joined:
    Sep 12, 2012
    Posts:
    1
    Try to put "Rigidbody" component on your game object (Rigidbody 2D if you make 2D Games), either on the one that collides other object or the one that gets collided by other object.