- Home /
On raycast hover change crosshair image
So I'm creating a pick up system similar to H1Z1 whereby providing you're in the triggerzone & a raycast is hovering / colliding with a object then it will change the cross from the simple crosshair to a picture of a hand to signify you can pick this object up.
I have this running between 2 scripts whereby when player is in OntriggerStay. Meaning if they're in the triggerzone of the object then it will set a bool in the raycast script to true.
If the raycast bool is true and the raycast hovers / collides with the object providing it has the correct tag then it will deactivate the crosshair image & activate the hand image.
And when the player isn't in the triggerzone / raycast is not colliding with object then it will revert from hang image to main crosshair.
However for some reason when I run debugs it will detect that the bool has been set to true but it won't actually let the raycast collide with the object.
Please note I have 2 raycasts. 1 for hovering over the object & 1 for clicking to grab the object.
Below is the two scripts if anyone can figure out how I can fix this bug would be greatly appreicated
Raycast Script
private float range = 300f;
public PickUpMatches _Matchbox;
public GameObject _HandImage;
public GameObject _CrossHairImage;
public BatteryPickUp _Battery;
public bool canHover = false;
// Update is called once per frame
void Update()
{
_Matchbox = FindObjectOfType<PickUpMatches>();
RaycastHit fireHit;
RaycastHit hoverHit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hoverHit, range))
{
Debug.DrawRay(ray.direction, hoverHit.point, Color.green);
if (canHover == true)
{
// Debug.Log("canHover is true");
if (hoverHit.collider.tag == "MatchBox" || hoverHit.collider.tag == "Battery")
{
Debug.Log("I'm looking at matchbox or Battery");
_HandImage.SetActive(true);
_CrossHairImage.SetActive(false);
}
}
else
{
Debug.Log("Not looking at anything");
}
// else if (hoverHit.collider.tag != "MatchBox" || hoverHit.collider.tag != "Battery")
// {
// Debug.Log("not looking at anything");
// _HandImage.SetActive(false);
// _CrossHairImage.SetActive(true);
//}
}
if (Input.GetButton("Fire1"))
{
if (Physics.Raycast(ray, out fireHit, range))
{
Debug.DrawRay(ray.direction, fireHit.point, Color.red);
if (fireHit.collider.tag == "Matchbox")
{
_Matchbox.AddMatch();
}
else if (fireHit.collider.tag == "Battery")
{
_Battery.AddBatteries();
}
Debug.Log(fireHit.transform.name);
}
}
}
Picking up Script
public int _matchesAmount = 1;
public RayCast_Pickup_Items item;
void Start()
{
item = FindObjectOfType<RayCast_Pickup_Items>();
}
void OnTriggerStay(Collider mat)
{
if (mat.tag == "Player")
{
item.canHover = true;
}
else if (mat.tag != "Player")
{
item.canHover = false;
}
}
public void AddMatch()
{
Checkpoint temp = GameObject.FindGameObjectWithTag("Checkpoint").
GetComponentInChildren<Checkpoint>();
temp.AddMatches(_matchesAmount);
Destroy(gameObject);
}
I've also noticed when playing the scene that my raycast seems to be appear below the player model and not displaying properly.
How can I fix this raycast?
You could probably make an empty child object positioned in the correct place where you want the ray to shoot from and then shoot from that ins$$anonymous$$d.
I had a similar situation, though I'm not sure if we had the same problem. Initially I wanted to just use On$$anonymous$$ouseEnter and On$$anonymous$$ouseExit, however I had to solve $$anonymous$$e just by using RaycastAll due to the ray colliding with other invisible colliders and I didn't want to use a mask layer for other reasons.
Try and debug log all the names of the colliders of what your ray is hitting, there might be something in the way.
Answer by Dragonfly3r · Dec 03, 2016 at 09:40 AM
I managed to fix this now.
The raycast image was because I had the raycast drawline originating from from area and the crosshair stuff is all fixed now.
In the end I ended up creating another script called TriggerArea
All this script did was bound it self to a empty gameobject with a sphere collider as trigger.
In the code was simple collision detection for OnTriggerStay which would activate a bool in the checkpoint script & OnTriggerExit which would deactivate the bool in the checkpoint script.
Thus resulting in the pickup script only realistically needing that AddMatch method.
The changes to the raycast script are as follows:
private float range = 500f;
public GameObject _HandImage;
public GameObject _CrossHairImage;
public Camera cam;
public bool canHover = false;
void Start()
{
_CrossHairImage.SetActive(true);
_HandImage.SetActive(false);
}
void Update()
{
RaycastHit hit;
Ray ray = cam.ViewportPointToRay(new Vector2(.5f, .5f));
if (Physics.Raycast(ray, out hit, range))
{
Debug.DrawRay(ray.origin, ray.direction, Color.green);
if (canHover == true)
{
if (hit.collider.tag == "Matchbox" || hit.collider.tag == "Battery")
{
_HandImage.SetActive(true);
_CrossHairImage.SetActive(false);
if (Input.GetButton("Fire1"))
{
if (hit.collider.tag == "Matchbox")
{
hit.collider.gameObject.GetComponent<PickUpMatches>().AddMatch();
canHover = false;
_HandImage.SetActive(false);
_CrossHairImage.SetActive(true);
}
else if (hit.collider.tag == "Battery")
{
hit.collider.gameObject.GetComponent<BatteryPickUp>().AddBatteries();
canHover = false;
_HandImage.SetActive(false);
_CrossHairImage.SetActive(true);
}
}
}
else
{
_HandImage.SetActive(false);
_CrossHairImage.SetActive(true);
}
I also had a small bug whereby the matchboxes were picking up together this was because it was automatically assigning a single matchbox to the player. So I changed this from having a script reference variable as:
public PickUpMatches _matches
void Start()
{
_matches = gameObject.GetComponent<PickUpMatches>();
}
if (hit.collider.tag == "Matchbox")
{
_matches.AddMatch();
}
To this:
I removed the mention in the start method and the script reference and simply updated this:
if (hit.collider.tag == "Matchbox")
{
hit.collider.gameObject.GetComponent<PickUpMatches>().AddMatch();
}
I also had a bug of whereby it wasn't firing that hit.collider.tag == "Matchbox" this was down to a simple misspelling of the word matchbox in the tag.
Had called in tag MatchBox and matchbox in the code. So the code was working just 1 letter I didn't notice in the code.