Hello
I have a big problem. I like to make my script door active from a distance. the situation is that my door has a script to be open when i press a keyboard such us i in my case. but i like that this script be active when the player is near the door and not from the bigenning of the game.
I tried a simple script with raycast but that dosn't work
this is the script
var rayCastLength = 5;
function Update()
{
var hit : RaycastHit;</p>
//check if we're colliding
if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
{
//...with a door
if(hit.collider.gameObject.tag == "Port 1")
//open the door
if(Input.GetKeyDown("i"))
animation.Play("port 1");
}
You don't need a RayCast for that. You can use a simple distance check:
var triggerDistance:float=5;
if((door.transform.position-transform.position).sqrMagnitude<triggerDistance*triggerDistance){
// check for key event etc.
}
(Note you should always use sqrMagnitude and the square in the comparison, instead of magnitude, due to performance reasons)
Another method would be to create a collider around your door, and only test for the key event if the player is inside that collider:
function OnTriggerStay(other:Collider){ // not sure if that's the correct function - check the manual/tutorial/examples
// check for key event etc.
}
A note regarding your code, you always cast your ray straight forward, so you will only get a hit if you actually directly look at the door(-collider), which might be the reason your code doesn't seem to work.