Change object position on trigger enter

Hi,

I have a door which can open and close using a drag and rigidbody script. Sometimes the door gets stuck in a wall and it’s a pain to get this fixed as this is most likely a physics bug.

What I’m trying to achieve is that once the door enters the wall (wall is collider), it must change to a specific position.

Basically my script saves the current X,Y,Z position/rotation. Once the player opens the door and the door gets stuck in the wall, it must get reset to the original position.

I tried to do with my following script, but I also get the following error. Can someone help me out fixing this issue? Resetting the position works, but how do I reset the rotation?

Error: Cannot convert ‘UnityEngine.Vector3’ to ‘UnityEngine.Quaternion’. This is about the last line in the script that starts with “transform.rotation” in the onTriggerEnter function.

Script:

var doorPosX : float;
var doorPosY : float;
var doorPosZ : float;
var doorRotX : float;
var doorRotY : float;
var doorRotZ : float;

function Start() {
	//store all current position and rotation for the door
	doorPosX = transform.position.x;
	doorPosY = transform.position.y;
	doorPosZ = transform.position.z;
	doorRotX = transform.rotation.x;
	doorRotY = transform.rotation.y;
	doorRotZ = transform.rotation.z;
}

function OnTriggerEnter (door : Collider) {
	if (door.gameObject.tag == "door") {
		Debug.Log("Reset");
		//if door gets stuck in wall, teleport door back into original position.
		transform.position = Vector3(doorPosX,doorPosY,doorPosZ);
		transform.rotation = Vector3(doorRotX,doorRotY,doorRotZ);
	}
}

It would help if we knew what line it was.

for starters

transform.position.x = Vector3(doorPosX,doorPosY,doorPosZ);

should be

transform.position = Vector3(doorPosX,doorPosY,doorPosZ);

The two other answer are “mostly” correct but usually it’s more convenient to use a Vector3 variable to store the initial position. For the rotation it’s a bit tricky :wink: the rotation property is a Quaternion. So either store the rotation as quaternion or use eulerAngles.

A Quaternion(wikipedia) doesn’t work with angles in degree. It has 4 components (not just 3) which represents a rotation axis and a rotation around this axis. You usually don’t mess around with the internal components of a quaternion unless you know how they work.

So do something like:

var doorPos : Vector3;
var doorRot : Quaternion;

function Start()
{
    doorPos = transform.position;
    doorRot = transform.rotation;
}

function OnTriggerEnter (door : Collider)
{
    if (door.gameObject.tag == "door")
    {
       transform.position = doorPos;
       transform.rotation = doorRot;
    }
}

The problem is that you’re trying to assign a Vector3 into position.x, which is a single float. Instead what you want to do is assign your Vector3 into transform.position, which is also a Vector3.

transform.position = Vector3(doorPosX,doorPosY,doorPosZ);