I have a portal mechanic in my project. I want it so when the Player collides with the portal, not only does it send them out through the other portal, but it also spins them 180 degrees to the left/right.
I have some code working. The portal teleport function works perfectly, but the rotating the player part is the issue.
Say I’ll walk into the BLUE portal in a North direction, When I hit that BLUE portal, I get sent out the RED portal. I do turn 180 degrees for half a second, but then the character auto-spins itself back in the direction I entered the BLUE portal.
How would I rectify this spin problem?
Pretty new to C# so it’s a learning process for me.
Photos attached of code and a [quick video of the auto-spin problem.][1]
!
[2]
![CODE IMGUR.NET][3]
IMGUR LINK =: https://i.imgur.com/SV7HiRF.png
-
Cheers all.
[1]: https://youtu.be/mH_fXS65JTM
[2]: /storage/temp/133306-screen-shot-2019-02-18-at-70149-pm.png
[3]: https://i.imgur.com/SV7HiRF.png
When you copy code - use the “Coder Sample” button, and then copy and paste.
Anywho, your code to rotate should work. So, the problem is elsewhere. I suspect it’s in your playerMotor. The user is pressing forward but in the instant you teleport , forward is toward the blue portal. A few things you could do:
First, instead of having the portal change the player’s orientation with a rotation, have it tell the playerMotor to use the rigidbody to do this… SendMessage will work fine for this. On the player motor perform your rotation and disable control input for a split second to allow the player to reorient themselves.
Second thing to try: make your rotation more “generic” … here’s what I mean… let’s assume you want the player to face in the forward direction of the portal regardless of which angle they came in from (they could hit from the sides, in which case you probably don’t want to spin 180 degrees). So instead, set the rotation to be the forward direction of the portal (or -1* it if they are facing the otherway).