slide open door by using transform coordinates

Hi,

I’ve been trying to slide open a door object by using the following command:

function OnMouseDown() {

transform.position = (-18.3864, 0, 0);

}

Is there any better way to move an object once from one specific location to another.

Please help

Thanks

You could translate the position so if you don’t want it to jump to the position. This way might be a little complicated but it works without too many conditionals.

var speed = 6;

var i : float = 0.0;
var canClickDoor = true;
var startPos : Vector3;

function OnMouseDown () {
     if(canClickDoor) {
          SlideDoor();
          canClickDoor = false;
          startPos = transform.position;
     }
}

function SlideDoor () {
	while(i<= 1) {
		transform.position = Vector3.Lerp(startPos, Vector3(-18.3864, 0, 0), i);
		i += Time.deltaTime * speed;
		yield;
	}
}

edit: And this is a little off topic, but I just like how it looks. Other ways you could find a way to use would be transform.Translate, or if you add a character controller (which would be strange for a door so I wouldn’t recommend it) you could use GetComponent(CharacterController).Move(). There are a maybe a few other ways you could approach it also, but I won’t get into it so as not to overwhelm you in your first post.

hello Peter. i am trying this code with box collider trigger. can you explain me how can i put this code?

Create a js script, copy this code into it. Attach it to your object.

i did it. but i want when i enter box collider open door. not click

Oh, then change OnMouseDown to OnTriggerEnter

it is not working with it. when i create trigger and put this script to collider it is not working

kulmarat2001 I am trying the same thing :confused: have you been able to sort it out ? let me know please. thanks