- Home /
how to cast a cross hair on every object?
hey guys, so i'm trying to put a simple cross hair using ray cast on every object in the scene who has colliders.the (game view is third person) i have 2 questions how can i tell the cross hair to be appear only on objects with colliders and if the gun is aimed at empty space the cross hair should be disappear. and how i can stop the cross hair image from jittering? when i project a cross hair on objects it constantly get on and off.. how to i stop that? i created a GIF file to show to problem. http://s6.uplod.ir/i/00921/e8d9tv3deu4z.gif
and here is the script im using to make a laser beam and put the cross hair at end of laser.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour {
public float range;
private LineRenderer lr;
public Transform crosshair;
public LayerMask layersForLaser;
// Use this for initialization
void Start () {
lr = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update ()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
lr.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, range,layersForLaser))
{
lr.SetPosition(1, hit.point);
crosshair.transform.position = (hit.point);
}
else
lr.SetPosition(1, ray.GetPoint(range));
}
}
Answer by Koyemsi · May 08, 2018 at 12:05 PM
To disable your crosshair when the ray cast doesn't hit anything, you could simply disable it in your else statement, after line 34 :
crosshair.SetActive(false);
(or disabling its renderer). And of course you should reactivate / re-enable it at ray cast hit (after line 30).
Don't know for the jittering, but I think you do not need to put hit.point between brackets on line 30.
thanks man that problem solved but still dont know what to do about the jittering .maybe it's because of it's shader?
Glad I could help !
For the jittering issue, I have poor idea. What kind of object is your crosshair ? Is it a sprite ? Then maybe it's jittering because of its 2D nature : when "projected" on the surface it's kind of "melting" with it (not sure if I'm clear). $$anonymous$$aybe you should try to offset it just a little, in order to place it a few "pixels" before the surface ? For example, if your ray cast was 10 units long, you would place your crosshair at 9.99f of the ray.
Another solution might be to put it in UI, maybe in a Canvas set to Camera Space ? Or make your crosshair a 3D mesh with some thickness ? These are really ideas on the fly, which may be noobish...
actually i'm using a quad with no collider and an additive particle shader applied to it and my texture for cross hair is a sprite with transparent alpha layer. i also used an image ins$$anonymous$$d of a quad but still no matter what kind of shader i use it always jitter. i think displaying the cross hair a few units further than hitting point would be fine , but dont know how to do that through script here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : $$anonymous$$onoBehaviour {
public float range;
private LineRenderer lr;
public GameObject crosshair;
public Layer$$anonymous$$ask layersForLaser;
// Use this for initialization
void Start () {
lr = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update ()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
lr.SetPosition(0, ray.origin);
if (Physics.Raycast (ray, out hit, range,layersForLaser)) {
crosshair.SetActive (true);
lr.SetPosition (1, hit.point);
crosshair.transform.position = (hit.point);
crosshair.transform.rotation = Quaternion.LookRotation (hit.normal);
}
else if (hit.collider == null) {
lr.SetPosition (1, ray.GetPoint (range));
crosshair.SetActive (false);
}
}
}
Your answer
Follow this Question
Related Questions
crosshair help needed 2 Answers
Crosshair help 1 Answer
Question about a space shooter crosshair targeting script 1 Answer