Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by georgetowersassets · Nov 15, 2020 at 12:09 PM · raycastsetactive

Set Object as active if Raycast hits nothing

I have a Grappling Hook mechanic which can be performed when aiming (holding Fire2). I have a regular white crosshairs for when the player isn't aiming. A green one for when aiming and the raycast hits an object with the correct Tag. I also have a red crosshairs for when the player is aiming and hits either something with the wrong tag or nothing at all.

The problem is that when I start aiming while looking at something with the wrong tag (not even mentioning looking at the sky yet), the crosshairs doesn't change to red, if I'm looking at the right tag and start aiming, the crosshairs changes to green and changes back to white when releasing Fire2 or using the Hook, which is exactly the behavior I want.

But if aiming to the right tag, then I rotate towards the wrong tag it won't change to red crosshairs, if I release Fire2 with the green crosshairs looking at the wrong tag (first it should've changed to red), it stays stuck and doesn't change back to white until I look at something with the right tag, then hold and release Fire2

I have set for debugging purposes a raycastHitObject which retrieves the object hit by the raycast if it also contains the right tag, else the object stays null. I also set a bool foundTarget which turns to true if the raycast hits the right tag, if it doesn't hit anything or hits the wrong tag, the bool will stay false.

Both of these work as expected and update in real time in play mode. I've tried checking if the bool is true/false and if object !/null (also tried using a placeholder instead of null and replace with the target if hit by raycast and checking if object != placeholder also with no luck) to trigger my desired behavior with no luck.

 public class GrapplingHook : MonoBehaviour
 {
     #region Variables
     //GameObjects
     public GameObject Hook;
     public GameObject redCrosshairs, targetFoundCrosshairs, regularCrosshairs;
 
     //Dynamic GameObjects (changed on the fly)
     public GameObject raycastHitObject;
 
     //Misc References
     public RaycastHit hit;
     public Camera raycastCamera;
 
     //Bools
     [SerializeField] bool isAiming;
 
     //debug bools
     public bool targetFound;
     #endregion
 
 void Update()
     {
         Physics.Raycast(raycastCamera.transform.position, raycastCamera.transform.forward, out hit, maxDistance);
        Debug.DrawLine(raycastCamera.transform.position, hit.point, Color.red);
 
         if (hit.transform == null || hit.transform.tag != "Hookable")
         {
             raycastHitObject = null;
             targetFound = false;
             return;
         }
 
         if (hit.transform.name != null && hit.transform.tag == "Hookable")
         {
             raycastHitObject = hit.transform.gameObject;
             targetFound = true;
          }
 
             if (!isAiming)
             {
                 redCrosshairs.SetActive(false);
                 targetFoundCrosshairs.SetActive(false);
                 regularCrosshairs.SetActive(true);
             }
 
             if (isAiming && targetFound)
             {
                 regularCrosshairs.SetActive(false);
                 redCrosshairs.SetActive(false);
                 targetFoundCrosshairs.SetActive(true);
             }
             
             if (isAiming && !targetFound)
             {
                 regularCrosshairs.SetActive(false);
                 targetFoundCrosshairs.SetActive(false);
                 redCrosshairs.SetActive(true);
             }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Raycasting + setActive() 1 Answer

Highlighting with a Raycast 1 Answer

Issues With SetActive 0 Answers

Ray-cast only detecting tags in debug.log and not in a if statement 0 Answers

Vector3.Distance returning offset values due to down force interference. 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