- Home /
active script from distance
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;
//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");
}
}
Please Help;
Answer by Wolfram · Aug 07, 2010 at 03:34 PM
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.
Answer by incitement · Aug 07, 2010 at 06:41 PM
I try it but it dosn't work with me. i'm new with scripting in unity so i found a problem to write a script. Thanks for your help
Answer by Borgo · Jan 11, 2011 at 02:02 PM
I have created a script that move a door, you can alter to rotate: Some variables are in portuguese (I'm from Brazil, sorry), but I explan each one:
Person: The character that you control.
Distancia: Min distance to open door.
toWhere : To where the door would move e.g:.
Up x:0 y:2 z:0,
Left: x: -5 y:0 z:0
Misc: x:5 y:2 z:-3
velocidade: Velocity
var person : Transform; var distancia : float; var toWhere : Vector3; var velocidade : float;
private var iniPosition : Vector3; private var endPosition : Vector3;
function Start(){ iniPosition = transform.position; endPosition = iniPosition+toWhere; }
function Update () {
var dist = Vector3.Distance(person.position, iniPosition);
if(dist < distancia){
transform.position.x = transform.position.x+((endPosition.x-transform.position.x)*Time.deltaTime*velocidade);
transform.position.y = transform.position.y+((endPosition.y-transform.position.y)*Time.deltaTime*velocidade);
transform.position.z = transform.position.z+((endPosition.z-transform.position.z)*Time.deltaTime*velocidade);
}else{
transform.position.x = transform.position.x+((iniPosition.x-transform.position.x)*Time.deltaTime*velocidade);
transform.position.y = transform.position.y+((iniPosition.y-transform.position.y)*Time.deltaTime*velocidade);
transform.position.z = transform.position.z+((iniPosition.z-transform.position.z)*Time.deltaTime*velocidade);
}
}
Your answer
Follow this Question
Related Questions
Raycast hits a collider and halts. 1 Answer
Raycasts hit triggers disabled - force collision for specific raycast 1 Answer
raycast ignore trigger colliders? 5 Answers
Can't click gameobject when over another trigger? 1 Answer
Why do my bullets only destroy themselves sometimes after colliding with the wall? 0 Answers