0
\$\begingroup\$

Here is my code:

using UnityEngine;
using System.Collections;

public class Crosshair : MonoBehaviour {
    public Camera cam;
    RaycastHit hit;
    Quaternion rot;
    // Update is called once per frame
    void Update ()
    {
        int x = Screen.width / 2;
        int y = Screen.height / 2;


        Ray ray = cam.ScreenPointToRay(new Vector3(x,y,0));
        Debug.DrawRay(ray.origin, ray.direction * 100, new Color(1f, 0.922f, 0.016f, 1f));
        rot = Quaternion.FromToRotation(Vector3.up, hit.normal);

        if (Physics.Raycast(transform.position, transform.forward, out hit))
        {
            Debug.Log("Now trying to locate enemy...");
           if(hit.transform && hit.transform.tag == "Enemy")
            {
                Debug.Log("Enemy Spotted!!");
            }
            else
            {
                Debug.Log("I tried bro...");
            }
        }


    }
}

as I can imagine if(Physics.Raycast(transform.position, transform.forward, out hit)) is always false.

What am I not doing correctly?

\$\endgroup\$
13
  • \$\begingroup\$ Your code seems to be working, double check the colliders of enemy \$\endgroup\$ Feb 17 '16 at 9:44
  • \$\begingroup\$ We do not know, how did you debugged to know that the ray really hits the "enemy". If it was just visually, there is your problem. You always have to check the values rather then the visuals. Of course the ray is going through your object. But as long as there is no collider attached to the "enemy" object, the ray hit can't detect it because it doesn't know against what objects it should test colliding with. \$\endgroup\$ Feb 17 '16 at 9:50
  • \$\begingroup\$ @YoshSynergi Hmm, I checked in scene view where the ray is visible. The "target" object is 3d cube, with Box Collider, with Enemy as name & tag, and on Default Layer. \$\endgroup\$
    – Valamorde
    Feb 17 '16 at 10:07
  • \$\begingroup\$ @HamzaHasan What should I be checking for? \$\endgroup\$
    – Valamorde
    Feb 17 '16 at 10:11
  • \$\begingroup\$ Check the colliders and tag, if both are configured properly or not \$\endgroup\$ Feb 17 '16 at 10:41
0
\$\begingroup\$

The code works fine, what fixed the problem was clearing the cache! I've been working on this project for so many hours that it probably clogged out!

EDIT: To clear the cache I exited Unity, located my Unity's cache folder and deleted the contents. Loaded Unity up again and voila!

\$\endgroup\$
7
  • \$\begingroup\$ What does clearing the cache mean in terms of Unity? \$\endgroup\$
    – House
    Mar 31 '16 at 21:45
  • \$\begingroup\$ @Byte56 I exited unity found my cache folder and deleted its contents, when I loaded it up again it was working. No idea how or why. \$\endgroup\$
    – Valamorde
    Apr 1 '16 at 17:22
  • \$\begingroup\$ Probably just a rebuild of the project would have worked. \$\endgroup\$
    – House
    Apr 1 '16 at 17:23
  • \$\begingroup\$ @Byte56 I tried that, it wouldn't work, I really have no idea what changed to be honest! \$\endgroup\$
    – Valamorde
    Apr 1 '16 at 17:40
  • \$\begingroup\$ Huh, well, please update your answer to include that information. \$\endgroup\$
    – House
    Apr 1 '16 at 17:41

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.