x


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;

more ▼

asked Aug 07 '10 at 03:15 PM

incitement gravatar image

incitement
30 17 17 22

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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);
                }

}
more ▼

answered Jan 11 '11 at 02:02 PM

Borgo gravatar image

Borgo
998 9 13 25

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 07 '10 at 03:34 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Aug 07 '10 at 06:41 PM

incitement gravatar image

incitement
30 17 17 22

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1682
x1525
x980

asked: Aug 07 '10 at 03:15 PM

Seen: 1009 times

Last Updated: Aug 07 '10 at 03:22 PM