- Home /
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).
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.
Note : I put your code inside your question to keep things clear.
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 ;
}
@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.
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
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
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;
}
}
}
}
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
Follow this Question
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