- Home /
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;
}
}
}
}
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;
}
pls show me where to declare the lastTarget variable and what to assign it at the start.
based on your other question i think you figured it out yourself
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
Follow this Question
Related Questions
Raycast not visible 1 Answer
Raycast returns null for no apparent reason 0 Answers
Raycast goes through 1 Answer
How can I can I cast a ray from a gameobject? 1 Answer
Raycast won't hit Collider 1 Answer