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 hyderox · May 06, 2012 at 04:36 PM · raycastcolortargetray

Help me in Targeting with Raycast please.

I want to make a script what selects a transform as target what is hitted by the ray. I want to turn the Target to red, and when the Ray isn't colliding with it, then it turns back to the original color. I made this script, but it is not what I want.

using UnityEngine; using System.Collections;

public class Targeting : MonoBehaviour {

 public Transform target;
 public int range = 20;

 void Update ()
 {
     Debug.DrawRay(transform.position ,transform.forward*range,Color.red);
     Vector3 forward = transform.TransformDirection(Vector3.forward);

     RaycastHit hit;
     if (Physics.Raycast(transform.position, forward, out hit))
     {
         target = hit.transform;
         target.renderer.material.color = Color.red;
     }
     else target.renderer.material.color = Color.grey;
 }

} How can i turn the previous target's color back to the original when i chose another target?

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 kolban · May 06, 2012 at 04:42 PM 0
Share

This is a duplicate question of: http://answers.unity3d.com/questions/249016/how-can-i-chose-the-previous-target.html

avatar image aldonaletto · May 06, 2012 at 05:29 PM 0
Share

Question "unclosed" due to new information added

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · May 06, 2012 at 05:44 PM

This may become complicated because the ray may hit many different objects, and you can't keep track of which object had which color. You can try the following: save the current object reference and color; next Update, if the hit object changed, set the old object color before updating the current object/color - like this:

using UnityEngine; using System.Collections;

public class Targeting : MonoBehaviour {

 public Transform target;
 public Color targetColor; // save the original color
 public int range = 20;

 void Update (){
     RaycastHit hit;
     // use transform.forward directly instead of calculating the forward direction:
     if (Physics.Raycast(transform.position, transform.forward, out hit)){
         // if something hit, check if it's a new object:
         if (hit.transform != target){ // if so,
             RestoreColor();           //  restore the previous object color...
             target = hit.transform;   //  update the target object...
             targetColor = target.renderer.material.color; // and save its color
             target.renderer.material.color = Color.red; // then paint it red
         }
     }
     else { // if nothing hit, restore the last object (if any) original color:
         RestoreColor();
     }
 }

 void RestoreColor(){
     if (target){ // if there exists a previous target, restore its original color:
         target.renderer.material.color = targetColor;
     }
 }

}

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i chose the previous target 2 Answers

Raycast on touch 3 Answers

Problem with raycasting when checking neighbouring objects? 0 Answers

What is the best way to navigate through a grid based level using Raycasting ? 2 Answers

How could I combine melee weapons and rays? 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