change tag of object with physics.raycast

This script is attached to many gameObjects and prefabs.

Firstly, here is the code I need help with:

function Update(){
var fwd = transform.TransformDirection (-Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 1)) {
placedObject = true;
}
}

The problem is with the public boolean placedObject. I want it to be set true if there is something in front of an object (if there is an object in front, placedObject is true), but instead it is setting true to the object which is in front (if this object is in front of another object, placedObject is true).

Please format your code (use the 101010-Button). Also make sure to read the [docs][1] if you are not sure how raycasts work. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

–

1 Answer

1

Check physics.raycast doc, once you get that in order, check the condition when the raycast hits an object, when you get the returned “hit” object. Access, the tag component of it and change the tag.

ex.

// you find hit at ray cast point, return hit object
hit.tag = "hit by raycast" // this is the newly assigned tag