- Home /
Question by
ayockel90 · Mar 03, 2020 at 06:41 PM ·
2dtopdownhingejoint2d
How to detect if Hinge Joint 2D is in original position?
I've made a door using Hinge Joint 2D for a top down 2d shooter. If the player is near the door and presses E, it runs the below functions based on whether it's open or closed:
void openDoor(){
rb.bodyType = RigidbodyType2D.Dynamic;
doorAudio.PlayOneShot(doorOpen);
rb.AddForce((transform.position - Player.transform.position) * 100);
isOpen = true;
lastOpenTime = Time.time;
}
void closeDoor(){
rb.bodyType = RigidbodyType2D.Kinematic;
transform.rotation = startingRotation;
transform.position = startingPosition;
doorAudio.PlayOneShot(doorLock);
isOpen = false;
lastOpenTime = 0.0f;
}
I would like closeDoor() to happen automatically if the door swings back to its original position. I tried doing this in below code but it doesn't seem to work
void Start(){
isOpen = false;
lastOpenTime = 0.0f;
startingPosition = transform.position;
startingRotation = transform.rotation;
}
void Update(){
if(transform.position == startingPosition && transform.rotation == startingRotation && isOpen == true && (Time.time - lastOpenTime) > 1){
closeDoor();
}
}
How can I detect if the door has swung back closed?
Comment