In this case, there are two revolving doors, made from a simple hinge joint.
I want to make it so whenever you push one, the other also rotates the same, and vice versa(a two way relationship).
Methods i have attempted so far:
- Having a ‘my twin’ reference, and setting transform.rotation to my twin’s transform.rotation
The issue there is that it can only be 1 way(having them reference each other means neither will move when pushed by my other rigidbody character), and there are issues when it comes to objects [intentionally] obstructing the doorway (the doors will go though them since you’re setting the rotation directly)
- Another method, same idea, this time rigidbody.MoveRotation to hopefully avoid phasing through objects
Same sort of trouble and worse, seems to have made objects able to push their way though the door itself(after causing the door object to shake a bit, all things which didn’t happen previously. It may have to do with low masses, and this new code simply exacerbates the existing issue there. Investigating further, but for now i moved on since it’s still only a one-way relationship)
-
a new system attempted was based on some code i found here(which i appear to have lost the link for :/) :
Vector3 targetDelta = target.position - transform.position;
//get the angle between transform.forward and target delta
float angleDiff = Vector3.Angle(transform.forward, targetDelta);// get its cross product, which is the axis of rotation to
// get from one vector to the other
Vector3 cross = Vector3.Cross(transform.forward, targetDelta);// apply torque along that axis according to the magnitude of the angle.
rigidbody.AddTorque(cross * angleDiff * force);
This also did not seem to work(one door kept spinning around, while the other seems to grow ‘stubborn’, applying an ever-increasing amount of resistance as my characters push it around… it’s actually a kinda neat spring/catapult effect, but not what i’m after.
-
and finally, another solution I’m trying is:
Quaternion targetRotation = Quaternion.LookRotation(target.position - transform.position);
float targetAngle = targetRotation.eulerAngles.y;if (targetAngle < 180.0) { rigidbody.AddTorque(new Vector3(0, 1, 0) * verticalSpeed * Time.deltaTime); } else if (targetAngle > 180.0){ rigidbody.AddTorque(new Vector3(0, -1, 0) * verticalSpeed * Time.deltaTime); }
based on code from this topic: Using rigidbody.AddTorque to have character look at cursor - Unity Answers
but it too does not seem to work as desired (infact not working so well even on just one of the doors, as it does not seem particularly interested in matching it’s partner at all).
I am going to keep working on this, checking through these last two solutions further in hopes of getting something working(or indeed trying some earlier dodgy solutions i thought up, such as using the first one listed, and doing a hacky strategy of updating each on every second frame perhaps, or maybe going with a coroutine to see if that helps), but if anyone already solved such an issue and would be willing to share their answer… well, the help would definitely be appreciated.