I am attempting to implement a feature similar to Amnesia: The Dark Descent's that makes the player hold down the mouse button and drag forwards or backwards on a door in order to open it (if you are not familiar see Door Example at about 36:30).
I have the following code, and it sort of works, but the opening is jittery and bounces back and forth a bit, and I cannot figure out if I'm using transform.Rotate incorrectly:
if (previousMousePosition == garbageVector) // if this is the first frame, create a reference for previousMousePosition
previousMousePosition = Input.mousePosition;
else
{
// else find the difference and rotate
rotationAmount = (Input.mousePosition.x - previousMousePosition.x);
if (rotationAmount < 0)
this.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
else if (rotationAmount > 0)
this.transform.Rotate(Vector3.down * rotationSpeed * Time.deltaTime);
}
Note: this script is attached to the door in question.
Also, my code doesn't take into account which side of the door the player is on when it comes to determining rotation. Does anyone have any tips on where I might start with that?
The code in your question is too small to accomplish that. Any ways you can easily do that by changing the
transform.localEulerAngles
. The code I attached in this answer can do that. T test the the script, follow direction below. This also describe how to determine front and back doors.1.Create a simple Cube named name it Door.
2.Remove the
Box Collider
that is automatically attached to the newly created cube/Door.3. Select the Door, Right click on and click "Create Empty". Rename that new GameObject to
FrontCollider
. Make sure thatFrontCollider
is now the child of Door GameObject.4.Select
FrontCollider
, add BoxCollider to it. Position the Collider to the front of the Door(Front of camera).5.Duplicate
FrontCollider
and rename the new GameObjectBackCollider
. Then move theBackCollider
GameObject to the back of the Door GameObject. The best way to make sure that it aligns well is if the z position of theFrontCollider
is -2, set the z position ofBackCollider
to 2. So all you do is change the z position ofFrontCollider
andBackCollider
colliders to be opposite of each other.5.Create an empty GameObject and name it Door Parent Make sure it is not a child of any GameObject. We need to use this GameObject to change the pivot point of the Door to the left. So that the Door will actually rotate like a door (one side)instead of around the center of itself.
6.Use the Move Tool and Move the Door Parent Gameobject to the left side of the Door. Do NOT rotate it. Just move it. Once you think that wherever the Door Parent is now located is where the Door should rotate around, attach the
Door Script
below to the Door Parent GameObject not the Door GameObject.7.Drag the
FrontCollider
GameObject to the frontDoorCollider slot in the script. Drag theBackCollider
GameObject to the backDoorColliderin slot in the script. That's it.When standing in front of the door,
click
,hold
andmove
the mouse down, it will PULL the door open. Moving it up will close it.When stading in the back of the door,
click
,hold
andmove
the mouse up, it will PUSH the door open. Moving it up will close it.