0
\$\begingroup\$

I have an FPS controller and I want to change the camera when it collides with a box collider. I think I did it properly but it didn't work. Here's my code:

using UnityEngine;
using System.Collections;

public class change_camera : MonoBehaviour
{
  public Camera Playercamera;
  public Camera Topcamera;

  void OnCollisionEnter(Collision col)
  {
      if (col.gameObject.name == "FPSController")
      {
          Playercamera.enabled = false;
          Topcamera.enabled = true;
      }
  }
}

Thanks in advance.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ "it didn't work" is never enough information. In what exact way did it not work? Did you get an error message at compile time or runtime? Did it do something unexpected? Did it fail to do something expected? Have you used a debugger or log output to verify that the code you showed is being executed? Also, just in general, be very wary of finding/comparing GameObjects by name — this makes it very easy to accidentally break relationships in a way that leaves no obvious trace or error message. \$\endgroup\$
    – DMGregory
    Commented Apr 19, 2016 at 12:18
  • \$\begingroup\$ well.. it's the first time I make a game, so I'm sorry if I'm not being clear. well, by "it didn't work" i meant "not a single thing happened".. as if there's no code.. \$\endgroup\$ Commented Apr 19, 2016 at 12:22
  • \$\begingroup\$ so this means the if statement is not being true. Put a Debug.Log("In if"); inside the if and a Debug.Log("OnCollision got called"); outside the if and see which one is outputting to the the console. col.gameObject.name is the name of the object that is colliding with this script. My guess is that you have attached this script to the player so maybe you col.collider.gameObject.name to determine what you have hit. \$\endgroup\$
    – Uri Popov
    Commented Apr 19, 2016 at 12:26
  • \$\begingroup\$ @YassineBouzaiane Step 1: Debug your code. Log something to the console when you collide to see if it registers Step 2: Debug your code. Try to change the camera when you press a key or when you start the game. Step 3: Debug your code. If anything fails, look at where it might have failed. Maybe your game freezes, maybe you disabled collision, maybe your object doesn't have a collision box. Step 4: You figured it out and never had to ask a question on stackexchange. \$\endgroup\$
    – yyny
    Commented Apr 19, 2016 at 12:28
  • 1
    \$\begingroup\$ google "unity debugging". Besides, make sure BOTH objects have colliders (not collider2D's) and neither of them have Is Trigger checked, and that at least one of them has a Rigidbody. \$\endgroup\$ Commented Apr 19, 2016 at 12:57

1 Answer 1

1
\$\begingroup\$

As people said in comment, you need to provide more information. Here is a list of stuff you can check:

  • Are Playercamera and Topcamera properly set in the editor
  • Did you add your script to the objects you want to react to collision
  • Did you add a collider component to these objects
  • Did you check the "Is trigger" option (if yes, you should use OnTriggerEnter instead

Something you can try is to write a debug line in your function

void OnCollisionEnter(Collision col)
{
    Debug.Log("Collision detected");
    if (col.gameObject.name == "FPSController")
    {
        Playercamera.enabled = false;
        Topcamera.enabled = true;
    }
}

This will write the text "Collision Detected" in you console if the function is called properly

\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to check the Collision Message Tables to make sure collision messages are sent for the two types of bodies you have interacting. \$\endgroup\$
    – DMGregory
    Commented Apr 19, 2016 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .