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 Dragonfly3r · Nov 18, 2016 at 07:43 AM · c#raycastraycasthitpickupcrosshairs

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?

alt text

raycast-bug.png (57.7 kB)
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 roman_sedition · Nov 19, 2016 at 01:52 AM 0
Share

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.

avatar image roman_sedition · Nov 19, 2016 at 02:07 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dragonfly3r · Dec 03, 2016 at 09:40 AM

@roman_sedition.

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.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i use raycast hit to detect object by the object tag name ? 1 Answer

How do I edit springjoint components in script? 1 Answer

RE: RaycastHit and camera through walls 0 Answers

C# change an object tag wit raycasthit. 2 Answers

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag, and which should I use when? 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