1
\$\begingroup\$

Like the box collider is not active or exist. I added a box collider on the left soldier.

box collider

I'm using firstpersoncontroller and firstpersoncharacter as player but i can move through the soldier and when the mouse cursor is over the soldier it's not detecting it as "Interactable" object.

The only object that is detecting is the door behind the soldiers. Any other objects are not detecting using the ray cast. So i added a box collider to the soldier for testing but it's not working.

Can't figure out why only the door is detecting.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRaycast : MonoBehaviour
{
    Ray ray;
    RaycastHit hit;

    void Update()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 1000))
        {
            if (hit.transform.tag == "Interactable")
            {
                print("Hitted on: " + hit.transform.name);
            }
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Try setting "isTrigger" to false \$\endgroup\$ – Bálint Nov 9 '17 at 23:25
  • \$\begingroup\$ @Bálint setting it off make it work but why when running the game it's not detecting the soldier only if i move close enough to it ? And then if i will go far from it will keep detect the soldier but first time running the game it's not detecting it when the mouse is over the soldier. But if it's the door it will detect the door. Strange. \$\endgroup\$ – Daniel Lip Nov 9 '17 at 23:50
  • 1
    \$\begingroup\$ Do you happen to start very close to this soldier, where the ray might have its origin inside the collider, rather than outside? Rays only detect intersections on the way in, not on the way out. ;) \$\endgroup\$ – DMGregory Nov 9 '17 at 23:55
  • \$\begingroup\$ @DMGregory Might be too close since the game start in a room. Maybe i should find another way to interact with objects/NPC's. What other ways there are ? \$\endgroup\$ – Daniel Lip Nov 10 '17 at 0:10
  • \$\begingroup\$ Can we also see the door (scripts on it etc.) that is being detected? There may be more variables present (like layers or tags etc.). \$\endgroup\$ – John Hamilton Nov 10 '17 at 8:00
1
\$\begingroup\$

You'll need to add a rigid body to your game object as well. The physics implementation needs both the computer and the rigid body in order for it to work correctly.

https://docs.unity3d.com/Manual/class-Rigidbody.html

\$\endgroup\$
  • 4
    \$\begingroup\$ This is not quite true. A collider with no Rigidbody will ordinarily be treated as a static obstacle, and other GameObjects with Rigidbodies or Character Controllers will be blocked by it (the obstacle just won't move and react in response) \$\endgroup\$ – DMGregory Nov 10 '17 at 1:56
  • \$\begingroup\$ Ahhh, good to know. \$\endgroup\$ – Tashus Nov 10 '17 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.