- Home /
How do I change the value of a tag with raycasting?
I have code here that when executed changes the value of the tag, but only changes the tag back to it's original value if I keep the mouse hovering over the object I click on.
var turnOn : scriptDialogueDisplay;
turnOn = GetComponent(scriptDialogueDisplay);
//This is the raycast!
function Update() {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 100))
{
if (Input.GetMouseButtonDown(0))
{
if (hit.collider.tag == "NPC")
{
turnOn.isOn = true;
hit.collider.transform.tag = "ActiveNPC";
}
}
if (turnOn.isOn == false)
{hit.collider.transform.tag = "NPC";}
}
}
I want the transition between being ActiveNPC and being NPC to be instantaneous, but I have no idea how to do this...
Well, the code only seems to work if the Ray hits anything, so have you tried keeping a pointer to the NPC and then setting it's value once the Ray stops hitting it?
Something like..
var turnOn : scriptDialogueDisplay;
turnOn = GetComponent(scriptDialogueDisplay);
var tempNPC : Transform = null;
//This is the raycast!
function Update() {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, 100))
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if (hit.collider.tag == "NPC")
{
tempNPC = hit.collider.transform;
turnOn.isOn = true;
hit.collider.transform.tag = "ActiveNPC";
}
}
} else if (turnOn.isOn == false && tempNPC!=null)
tempNPC.tag = "NPC";
tempNPC = null;
}
}
I didn't test it, but it should probably work..
Your answer
Follow this Question
Related Questions
Raycast that ignores object with certain tags? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to instantiate gameobject on raycast collision. 1 Answer
Raycast collision detection on a 2d game 1 Answer
How can I check the line of sight to see if the player is visible to the enemy? 1 Answer