Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Raycasting problem, colliders not being recognized? 1 Answer

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

Accessing Raycast collider from another script 0 Answers

Raycast bug is making me go nnuts,Raycast bug is making me go nuts 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges