• 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 PaxForce · Oct 18, 2014 at 11:30 AM · raycastraycastingraycasthitraytargeting

how detect "OnRaycastOut"

When my characterMesh turns towards a cube, it turns gray. When I move my characterMesh the other way (he now faces a different cube), the new cube should turn gray. I have come that far. I would like the old cube to turn to it's original color. Any ideas?

 using UnityEngine;
 using System.Collections;
 
 public class DmgCube_scr : MonoBehaviour {
     
     Color originalColor;
     GameObject target;
     GameController_scr gc_scr;
     
     
     void Start () {
         originalColor = gameObject.renderer.material.color;
         gc_scr = GameObject.Find ("GameController").GetComponent<GameController_scr>();
     }
     
 
     void Update () {
 
         // ::::::::::::::::::::::::::::::::::::::::::::::: TARGETING :::::::::::::::::::::::::::::::::::::::::::
         Ray targetingRay = new Ray(transform.position, transform.TransformDirection (Vector3.forward));
         RaycastHit targetHit;
         
         if(Physics.Raycast (targetingRay, out targetHit, 1f))
         {
             if(targetHit.transform.gameObject.tag == "cube")
             {
                 target = targetHit.transform.gameObject;
                 target.renderer.material.color = Color.gray;
             }
         }
     }
 }

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 767_2 · Oct 18, 2014 at 11:43 AM

you can keep your last target and change it`s color if the new target is not the same

 if(targetHit.transform.gameObject.tag == "cube")
                  {
                      target = targetHit.transform.gameObject;
                      if(target!=lastTraget)
                             lastTraget.renderer.material.color = originalColor;
                      lastTarget=target;
                      target.renderer.material.color = Color.gray;
                  }
             else //when player looks at something else other than cubes
                  {
                    
                             lastTraget.renderer.material.color = originalColor;
                      
                  }

Comment
Add comment · Show 3 · 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 PaxForce · Oct 18, 2014 at 12:31 PM 0
Share

pls show me where to declare the lastTarget variable and what to assign it at the start.

avatar image 767_2 · Oct 18, 2014 at 02:40 PM 0
Share

based on your other question i think you figured it out yourself

avatar image PaxForce · Oct 18, 2014 at 03:26 PM 0
Share

no I didn't. I just came up with a nasty work-around. I would really like to know a better, more efficient way.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

Raycast not visible 1 Answer

Raycast returns null for no apparent reason 0 Answers

[c#] While loop not exiting, I have no idea why 2 Answers

RaycastHit always returns 0 1 Answer

Can't find a clone object with RayCast? [Solved] 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges