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?
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 ;
}
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 );
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]