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 /
avatar image
0
Question by h00man · May 08, 2018 at 11:01 AM · crosshair

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));
         
         
 
         
 }
 
 }

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
Best Answer

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.

Comment
Add comment · Show 7 · 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 h00man · May 08, 2018 at 03:50 PM 0
Share

thanks man that problem solved but still dont know what to do about the jittering .maybe it's because of it's shader?

avatar image Koyemsi h00man · May 08, 2018 at 04:51 PM 1
Share

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...

avatar image h00man Koyemsi · May 08, 2018 at 05:30 PM 0
Share

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);
         }
 
     
 }
 
 }
Show more comments

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

82 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 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 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

crosshair help needed 2 Answers

Crosshair help 1 Answer

Question about a space shooter crosshair targeting script 1 Answer

Fire projectie to crosshair in center of screen 2 Answers

CrossHair Automatically Tracking Object 1 Answer


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