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");
}
}
}
}
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.
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
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)
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 :(
Answer by Veerababu.g · Dec 18, 2015 at 11:04 AM
do you get the out put
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

Follow this Question
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