Search Unity

Started with Unity HELP!

Discussion in 'Scripting' started by mumzel99, Sep 16, 2018.

  1. mumzel99

    mumzel99

    Joined:
    Sep 16, 2018
    Posts:
    3
    Hey! I have started making a game using a tutorial. I followed the tutorial, but seem to have an issue. Whenever I collide into an object, the game doesn't restart but if I fall off the map I do. Help!

    https://gyazo.com/23fd0c0209285284088e419a7f183869
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Well what's your code for the collision event?
     
  3. Nama222

    Nama222

    Joined:
    Jul 25, 2017
    Posts:
    6
    To me, it seems like the "is Trigger" checkbox (of the box collider) of both (player and obstacle) are set false, so they can't go into each other, so your collision event isn't called. To fix this, there are several ways:

    1. The easiest way: Set the "is Trigger checkbox of one of them to true. Will work fine, if you want the game to restart directly.
    2. If you want the game to display a menu and not to restart directly, it's a little bit more complicated: Add a second slightly bigger box collider to the player and set its "is trigger" checkbox to true. To stop the obstacles from rotating after a collision you could either remove their Rigidbody (if not needed) or add this piece of code in the Start()-function of a script attached to the obstacles:
    Code (CSharp):
    1. Rigidbody rb = GetComponent<Rigidbody> ();
    2. rb.freezeRotation = true;
     
  4. mumzel99

    mumzel99

    Joined:
    Sep 16, 2018
    Posts:
    3
    This is for my PlayerCollision

    using UnityEngine;

    public class PlayerCollision : MonoBehaviour{

    public PlayerMovement movement; // Reference towards PlayerMovement script

    // This function runs when we hit another object.
    // We get info about the collision and call it "collisionInfo"

    void OnCollisionEnter (Collision collisionInfo)
    {
    // We checked if the object we collided with has a tag called "obstacle"
    if (collisionInfo.collider.tag == "Obstacle")
    {
    movement.enabled = false; // Disable the players movement
    FindObjectOfType<GameManager>().EndGame();
    }
    }
    }