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 Ego65 · Dec 17, 2015 at 08:41 AM · raycasthitrenderenableupdate functionwhile

how to display object while ! raycast hit is true ?

hey at all, i need some help (once more)

i want a plane with a crosshair texture to be displayed when the enemy object is hitten by a raycast from the player ( if foundhit = true) it basically works but not continiously while its being hitten. it's displayed for a short moment, then it disappears, then it is displayed again for a short moment (and so on) while the ray hits the enemy. seems it's because of the update funct. certainly i tried to set it to : while(foundhit = true) and certainly unity stucked.
how to achiefe that it is displayed while! the ray hits ?
thx previously

here's the code:
using UnityEngine; using System.Collections;

 public class Crosshair : MonoBehaviour {
     public Transform raytrf;
     public float raycastDist = 1000.0f;
     public string tagCheck = "Enemy";
     public bool foundHit;
     public bool checkAllTags = true;
     RaycastHit crosshair_hit;
     //private Renderer rend;
     public GameObject thisplane;
 
     // Use this for initialization
     void Start () 
     {
         //rend = GetComponent<Renderer> ();
         //rend.enabled = false;
     }
     
     // Update is called once per frame
     void Update () 
     {
         //bool foundHit = false;
         RaycastHit crosshair_hit = new RaycastHit();
         foundHit = Physics.Raycast(transform.position, transform.forward, out crosshair_hit, raycastDist);
         //Vector3 forward = raytrf.TransformDirection(Vector3.forward.normalized) * 1000;
         //Debug.DrawRay(raytrf.position,forward, Color.red);
 
         if (foundHit && checkAllTags && crosshair_hit.transform.tag != tagCheck)
         {
             foundHit = false;
             //rend.enabled = false;
         }
         if (!foundHit) 
         {
             thisplane.layer = LayerMask.NameToLayer ("Invisible");
             //rend.enabled = false;
             //Debug.Log("false");
         } 
         else 
         {
             if(foundHit) 
             {
                 thisplane.layer = LayerMask.NameToLayer ("Default");
                 //rend.enabled = true;
                 //Debug.Log("hit");
             }
         }
 
     }
 
 }
 
Comment
Add comment · Show 2
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 Veerababu.g · Dec 18, 2015 at 05:56 AM 1
Share

bool hitOnce;

if(foundHit==true&&hitOnce==false){ //do what stuff you need to do at only once.

hitOnce=true;

}

//also you can do like whnever it is hitting the collider just disable that .ray cast works only for colliders and triggers.

avatar image Ego65 Veerababu.g · Dec 18, 2015 at 10:38 AM 0
Share

thx for the tip! have to be true : the code was ok, my tags and layers of the laser projectiles of the enemy have been a mess my fault! :) seems so they did interact with the raycast thats why the crosshair was "blinking" after sorting them and change their settings in the layer collision matrix all works well.
but now i know what is meant by "save a reference"
another little step forward for "learning" :)

yours

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by hexagonius · Dec 17, 2015 at 12:49 PM

How about saving a reference to the hit object. In the raycast check, check the object reference for null. if it is, it's the first time you raycasted something, save that. if it is not null, check if your raycast still the same. enable crosshair on first encounter and disable on first other encounter (different object or nothing hit)

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 Ego65 · Dec 18, 2015 at 02:37 AM 0
Share

sorry but i do not really know what you mean (beginner) and how to do this. some example code would be helpful. meanwhile i was experimenting with a coroutine: if(foundHit).... waitforseconds(1). not sure if i did it right ( got no error) but the result was the same :(

avatar image
2

Answer by Veerababu.g · Dec 18, 2015 at 11:04 AM

do you get the out put

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 Ego65 · Dec 18, 2015 at 03:51 PM 0
Share

yes, all works well now. after sorting the tags and layers it wasn't even necessary to set up the bool hitOnce additionally. i''ll try to keep it $$anonymous$$d :)

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

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast.distance not working in a while loop 0 Answers

Unity keeps crashing when 'While' is used... 1 Answer

canvas render sorting error 0 Answers

Is it Possible to use visual effect graph with LWRP configured with 2D renderer 2 Answers

Create and reference assembly (.asmdef) files at editor-time 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