• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mihaaa_123_123 · Jan 11, 2016 at 04:54 PM · c#unity 5raycastingraycasthitrays

Unity Raycasting Issue

Hello! (C#) I have in my game first person player, with an empty object on the top (Used as a point from where raycast is used).

I have this script on the point:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class ObjectDetection : MonoBehaviour {
 
 
     public float rayDist = 3;
     public Text pickUpText;
     
 
     void Update()
     {
 
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
 
         if (Physics.Raycast (ray, out hit, rayDist))
         {
             Debug.Log ("hit");
             
             if(hit.collider.gameObject.tag == "test1") 
             {
             pickUpText.text = "Press E to pickup BLAH"; 
                 isHit = true;
                 Debug.Log("hit");
                 //hit.collider.gameObject.GetComponent<MeshRenderer>().enabled = false;
                 
 
 
             }
             
 
 
             //Only For Debug
             if(showHit == true)
             {
             Debug.DrawLine(ray.origin, hit.point);
             Debug.DrawLine(hit.point + (Vector3.up  / 2),hit.point + (Vector3.down / 2),Color.blue);
             Debug.DrawLine(hit.point + (Vector3.left / 2),hit.point + (Vector3.right / 2),Color.red);
             }
 
         }
         else 
         pickUpText.text = "";
         Debug.Log("not hit");
         
 
 
     }
 
 
 }

So when i play the game:

  • if the ray hits nothing -- the console prints "not hit"

  • if the ray hits an object with tag test1 -- the console prints "not hit", "hit", "not hit", "hit", "not hit", "hit".... and so on "

please tell me WHY? And how to enable the MeshRender when thy Ray leaves the object??

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Jan 09, 2016 at 09:53 PM

it's because you're ALWAYS printing "not hit" - you need to enclose the lines following the else in braces. as it is right now, the else statement is just for pickUpText.text = "";

easy mistake to make ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mihaaa_123_123 · Jan 13, 2016 at 07:38 PM 0
Share

Thanks! Now it works!! But the text blinks really fast. Why? And when I export the game to *.exe it's ok and not blinking.

Am I doing something wrong?

The complete Code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class ObjectDetection : $$anonymous$$onoBehaviour {
 
 
     public float rayDist = 3;
     public bool showHit;
     public Text pickUpText;
     public bool isHit;
     public GameObject hitObject;
 
     void Start () {
 
 
     }
     
     void Update()
     {
 
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
 
         if (Physics.Raycast (ray, out hit, rayDist)) { //Creates a RAY
             
             
             if (hit.collider.gameObject.tag == "test1") {
                 pickUpText.text = "Press E to pickup BLAH"; 
                 isHit = true;
                 Debug.Log ("hit");
                 hit.collider.gameObject.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
                 hitObject = hit.collider.gameObject; //Remember the object 
 
 
 
             }
 
 
             //hitObject.GetComponent<$$anonymous$$eshRenderer>().enabled = true;
 
 
 
 
             
             if (showHit == true) {
                     //Debug.DrawLine (ray.origin, hit.point);
                 Debug.DrawLine (hit.point + (Vector3.up / 2), hit.point + (Vector3.down / 2), Color.blue);
                 Debug.DrawLine (hit.point + (Vector3.left / 2), hit.point + (Vector3.right / 2), Color.red);
             }
 
         } 
         else 
         {
             pickUpText.text = "";
             Debug.Log ("not hit");
             isHit = false;
             hitObject.GetComponent<$$anonymous$$eshRenderer>().enabled = true;
         }
 
 
 
     }
 
 
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

An probably very easy question about Raycast system... 1 Answer

Item Collection with Raycast / C# 1 Answer

Rayscast from the GameObject that is not the player does not work 0 Answers

Raycast doesn't stay in one place 0 Answers

Accessing Raycast collider from another script 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges