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 EastAtom · Oct 02, 2017 at 03:13 PM · raycastingcolor changecalculation

Raycast distance change object color

Hi everyone, I wanted to get my player the object's color when he is near it, I'm new to scripting but I think it could be done with Raycasts: the smaller the distance, the stronger the player's color (as shown in the picture).

alt text

I 'm learning how to cast a ray, but how can I implement the color variation in the script?

I have this piece of code, I wanted to cast the ray from the object that is going to change color but it seems that the raycast work only if the script is attached to the camera...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Raycasting : MonoBehaviour {
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         RaycastHit hit;
 
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
 
         if (Physics.Raycast (transform.position, fwd, out hit, 20.0F))
         {
             //I checked if the ray found my object
             if (hit.collider.gameObject.name.Equals("object"))
                 print(hit.collider.gameObject.name);
         }
     }
 }




P.s.
Is it possible to change hue when the player go near another object?

Thanks.

concept.png (164.8 kB)
Comment
Add comment · Show 1
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 Hellium · Oct 02, 2017 at 05:40 PM 0
Share

Note : I put your code inside your question to keep things clear.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Hellium · Oct 02, 2017 at 03:20 PM

Try this :

  private Material material ;

 private Awake()
 {
     material = GetComponent<Renderer>().material ;
 }

 // ...

      RaycastHit hit; 
      Vector3 fwd = transform.TransformDirection(Vector3.forward);
 
      if (Physics.Raycast (transform.position, fwd, out hit, 20.0F))
      {
          //I checked if the ray found my object
          if (hit.collider.gameObject.name.Equals("object"))
         {
             float distance = hit.distance ;
             Color color = Color.Lerp(Color.blue, Color.white, distance / maxDistance ); // Where maxDistance is the max distance your character can be from the target. Put the value you want
             material.color = color ;
      }

Comment
Add comment · Show 1 · 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 Hellium · Oct 02, 2017 at 05:47 PM 0
Share

@EastAtom : I edited my answer and merged your code into $$anonymous$$e.

You say "it seems that the raycast work only if the script is attached to the camera". Where do you want your ray to be cast from ? Your character ? Use Debug.DrawRay to understand why casting your ray from elsewhere does not work.

avatar image
1

Answer by BlakeSchreurs · Oct 02, 2017 at 06:11 PM

A ray cast implies you're looking at the target. If that's not the case, and all you want to do is change color based on distance, you can make your code much easier and efficient by skipping the raycast entirely and use the vector3 distance method:

// Find target object here

float dist = Vector3.Distance(transform.position, target.position); Color color = Color.Lerp(NearColor, FarColor, dist / maxDist );

// Assign color here

https://docs.unity3d.com/ScriptReference/Vector3.Distance.html

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
avatar image
0

Answer by EastAtom · Oct 02, 2017 at 09:49 PM

Thanks to everyone! I really apprecciate your help, the code is working, althought the gradient transition is not that fluid and when the transition happens I expected the color to be more light I will try and improve it.

The second screenshot is the first gradient transition

Could it be related to the limit of blue gradations ?

 ![using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Raycasting : MonoBehaviour {
     public GameObject player;
     public GameObject target;
     // Use this for initialization
     void Start () {
        
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         RaycastHit hit;
 
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
 
         if (Physics.Raycast (transform.position, fwd, out hit, 20.0F))
         {
             if (hit.collider.gameObject.name.Equals("target"))
             {
 
                 float dist = Vector3.Distance(transform.position, target.transform.position);
 
                 Color color = Color.Lerp(Color.blue, Color.white, dist / 30);
                 player.GetComponent<Renderer>().material.color =  color
             }
         }
     }
 }][1]
 


[1]: /storage/temp/103017-untitled-1.jpg


untitled-1.jpg (109.7 kB)
Comment
Add comment · Show 2 · 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 EastAtom · Oct 04, 2017 at 08:17 PM 0
Share

When I look away from the target, the player is still blue, how can I return player color to white?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Raycasting : $$anonymous$$onoBehaviour {
     public GameObject player;
     public GameObject target;
     // Use this for initialization
     void Start () {
        
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         RaycastHit hit;
 
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
 
         if (Physics.Raycast (transform.position, fwd, out hit, 20.0F))
         {
             if (hit.collider.gameObject.name.Equals("nero"))
             {
 
                 float dist = Vector3.Distance(transform.position, target.transform.position);
 
                 Color color = Color.Lerp(Color.blue, Color.white, dist / 30);
                 player.GetComponent<Renderer>().material.color = color;
             }
             else 
             {
                 player.GetComponent<Renderer>().material.color = Color.white;
 
             }
 
 
 
         }
 
     }
 }
 
avatar image Hellium EastAtom · Oct 04, 2017 at 08:40 PM 0
Share
 void FixedUpdate () {
      RaycastHit hit;
      Color color = Color.white ;
 
      Vector3 fwd = transform.TransformDirection(Vector3.forward);
 
      if (Physics.Raycast (transform.position, fwd, out hit, 20.0F))
      {
          if (hit.collider.gameObject.name.Equals("nero"))
          {
              float dist = Vector3.Distance(transform.position, target.transform.position);
              color = Color.Lerp(Color.blue, Color.white, dist / 30);
          }
      }
      player.GetComponent<Renderer>().material.color = color;
  }

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

75 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

Related Questions

Bounds.IntersectRay() inaccurate? 1 Answer

Aim flashlight at cursor in third person 1 Answer

Trying to make a tractor beam 1 Answer

Is this way of making a raycast correct 2 Answers

How to measure rate of Raycast hit events? 2 Answers


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