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 /
avatar image
0
Question by NerdRageStudios · Jan 16, 2015 at 09:12 PM · raycastrendererraycasthit

Problem changing renderer back to original after changing it on raycast hit

Hi all, I have a script that highlights the object in the center of the screen by changing its renderer on hit and storing that object as the original target. It checks to see on hit if the object has a script or not and changes the renderer if it does.

the script swaps it back to its original one once the original target becomes null.

This works fine. the problem is when you have multiple objects and no gap between them in the view point, so effectively the raycast moves between objects and never becomes null, thus multiple objects all highlight.

I have tried to get around this, but to be honest I cannot figure it out. Please can someone help me swap back to the original shader, even if the raycast hit never becomes null?

Here is the code snippet I need to adjust:

 public class Player_Script_PickupAndMoveObject : MonoBehaviour {
 
     GameObject mainCamera;
     GameObject carriedObject;
 
     int screenX;
     int screenY;
 
     bool isCarrying;
 
     public float objectCarryDistance;
     public float cameraSmooth;
 
     GameObject targetObject;
     GameObject previousObject;
 
     // Use this for initialization
     void Start () 
     {
         mainCamera = GameObject.FindWithTag("MainCamera");
         screenX = Screen.width / 2;
         screenY = Screen.height / 2;
     }
     
     // Update is called once per frame
     void Update () 
     {
             Ray targetRay = mainCamera.camera.ScreenPointToRay(new Vector3(screenX, screenY));
             RaycastHit hit;
             if (Physics.Raycast(targetRay, out hit))
             {
                 Object_Script_Pickup p = hit.collider.GetComponent<Object_Script_Pickup>();
                 if (p != null)
                 {
                     targetObject = p.gameObject;
                     p.isTargetted = true;
                 }
                 else
                 {
                     Object_Script_Pickup targetScript = targetObject.GetComponent<Object_Script_Pickup>();
                     targetScript.isTargetted = false;
                 }
             }

Many thanks in advance for any help on this!

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

Answer by Deadcow_ · Apr 05, 2015 at 07:14 PM

 // Initialized here to not cause null exception at first update call
 Object_Script_Pickup[] cachedSelected = new Object_Script_Pickup[0]; 

 void Update()
 {
     Ray targetRay = mainCamera.camera.ScreenPointToRay(new Vector3(screenX, screenY));
 
     // All selected at this frame
     var currentHits = Physics.RaycastAll(targetRay)
         .Select(h => h.collider.GetComponent<Object_Script_Pickup>())
         .Where(o => o != null).ToArray();
 
     // There is newly selected objects (current selected except previously cached objects)
     var newHits = currentHits.Except(cachedSelected).ToArray();
     Array.ForEach(newHits, h => h.isTargetted = true);
 
     // This objects is not selected any more
     var wasSelected = cachedSelected.Except(currentHits).ToArray();
     Array.ForEach(wasSelected, h => h.isTargetted = false);
 
     cachedSelected = currentHits;
 }


Okay, I not tested this piece, but if there is a bug somewhere it wont be to hard to find out how to fix it ;)

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

26 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

Related Questions

Using Touch to Move Pong Paddles 2 Answers

How to Instantiate a line or a ray that goes through all colliders and send a message to them???? 1 Answer

RaycastHit.triangleIndex always returning -1 5 Answers

Ray is not being detected 0 Answers

Raycasting completely cancelling instantiation. 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