unity3d activation distance.

Hi,i have made a script,once i press “n’” button the cube stick to wall,what i want to do is,i want that only happen when i come close to cube not i can do that at any place of map,just when i come close to cube,then i can only press “n” button and then its gonna stick to wall,i know there needs to use raycast to apply action distance,but i cant do that,i dont have such experience,so im asking you for help,please help me :slight_smile:

P.S. More like pickup distance…or something.

Here is the script:

var jointExist : boolean;

function OnCollisionEnter(c : Collision) {
    if(!jointExist){
      var joint = gameObject.AddComponent(FixedJoint);
      joint.connectedBody = c.rigidbody;
      jointExist = true;
   }
}

function Update(){
   if(Input.GetKeyDown(KeyCode.N)){
      var joint = gameObject.GetComponent(FixedJoint);
      Destroy(joint);
      jointExist = false;
   }
}

Can anyone help me???

I believe that you want Vector3.Distance or magnitude.

if(Vector3.Distance(transform.position, cube.transform.position) < myDistance){ do something; }

Yeah,i’ve have seen this one,can you please modify mine script to make this work,cause i cant do this with my experience and i’ve been searching for this solution about days.Your help will be much appreciated. Thanks :slight_smile:

Hello?

bigmisterb,can you please help me then?

You should really try. :wink:

var myDistance: float = 20;

function Update(){
	if(Input.GetKeyDown(KeyCode.N)){
		if(Vector3.Distance(transform.position, cube.transform.position) < myDistance){
			if(jointExist){
				var joint = gameObject.GetComponent(FixedJoint);
				Destroy(joint);
				jointExist = false;
			}
		}
	}

}