Problem with Rotating Platforms in Unity 3D
-
Hello guys, I'm making a small 3D platformer game with Unity, and I got some serious troubles making the player to walk on the rotating platforms, actually it turns out that it is absolutely impossible for me, I really tried everything, I've read probably all articles in the web about rotating platforms, parenting and so on, but nothing seems to help, even though the problem looks very simple. What I'm doing is when the player jumps on the platform I make him a parent to that platform so he cam move with it, but in the moment I set him as a parent he turns at some degrees left or right(probably depending in the differences between the angle of the platform rotation and the angle of the player), here is a video that illustrates the problem: err - YouTube[^] I really have no idea why this happens and I'm so desperate nothing seems to work and if I can't fix it there is no point in continue making the game :(( Here is my code for parenting to the rotating platform:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;public class PlatformRide : MonoBehaviour
{
public GameObject _parent;
private GameObject _player = null;// Use this for initialization void Start() { transform.gameObject.GetComponent().isTrigger = true; } // Update is called once per frame void LateUpdate() { if (!Globals.GamePaused) { \_parent.transform.localRotation = transform.rotation; // rotate the parent with the platform } } void OnTriggerEnter(Collider col) { if (col.name == "CCLSSF\_player") { \_player = col.gameObject; \_player.transform.parent = \_parent.transform; } } void OnTriggerStay(Collider col) { if (col.name == "CCLSSF\_player") { \_player = col.gameObject; \_player.transform.parent = \_parent.transform; } } void OnTriggerExit(Collider col) { if (col.name == "CCLSSF\_player") { \_player.transform.parent = null; \_player = null; } }
}
I'm using the standard Unity FPS Controller, what could be wrong? Please help, thank you in advance.
-
Hello guys, I'm making a small 3D platformer game with Unity, and I got some serious troubles making the player to walk on the rotating platforms, actually it turns out that it is absolutely impossible for me, I really tried everything, I've read probably all articles in the web about rotating platforms, parenting and so on, but nothing seems to help, even though the problem looks very simple. What I'm doing is when the player jumps on the platform I make him a parent to that platform so he cam move with it, but in the moment I set him as a parent he turns at some degrees left or right(probably depending in the differences between the angle of the platform rotation and the angle of the player), here is a video that illustrates the problem: err - YouTube[^] I really have no idea why this happens and I'm so desperate nothing seems to work and if I can't fix it there is no point in continue making the game :(( Here is my code for parenting to the rotating platform:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;public class PlatformRide : MonoBehaviour
{
public GameObject _parent;
private GameObject _player = null;// Use this for initialization void Start() { transform.gameObject.GetComponent().isTrigger = true; } // Update is called once per frame void LateUpdate() { if (!Globals.GamePaused) { \_parent.transform.localRotation = transform.rotation; // rotate the parent with the platform } } void OnTriggerEnter(Collider col) { if (col.name == "CCLSSF\_player") { \_player = col.gameObject; \_player.transform.parent = \_parent.transform; } } void OnTriggerStay(Collider col) { if (col.name == "CCLSSF\_player") { \_player = col.gameObject; \_player.transform.parent = \_parent.transform; } } void OnTriggerExit(Collider col) { if (col.name == "CCLSSF\_player") { \_player.transform.parent = null; \_player = null; } }
}
I'm using the standard Unity FPS Controller, what could be wrong? Please help, thank you in advance.
If it works similar to WinForms, then the player would be confined to moving within that platform, not on top of it. I'd try adding some invisible container (Panel?) above the platform, same length as the platfrom, and a bit taller than the player. Next, set that as the parent. Not sure if that would help, but the terms you're using gives me hope that it'll work similar :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
If it works similar to WinForms, then the player would be confined to moving within that platform, not on top of it. I'd try adding some invisible container (Panel?) above the platform, same length as the platfrom, and a bit taller than the player. Next, set that as the parent. Not sure if that would help, but the terms you're using gives me hope that it'll work similar :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
Thank you Eddy, yes I did it in a similar way you described, fortunately last night when I was scrolling down Google results(again....) I found this article: Preserving the players rotation on rotating platforms : Unity3D[^] The user there experienced the exact same problem as mine and he managed to solve it by changing the 'localRotation' to 'rotation' in the MouseLook.cs class from the standard Unity FPS controller, here is the code for the fix(this is the last if... statement from LookRotation void:
if (clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);if (smooth) { character.localRotation = Quaternion.Slerp(character.localRotation, m\_CharacterTargetRot, smoothTime \* Time.deltaTime); camera.localRotation = Quaternion.Slerp(camera.localRotation, m\_CameraTargetRot, smoothTime \* Time.deltaTime); } else { //comment the original rotation: //character.localRotation = m\_CharacterTargetRot; //put this line to fix the rotation when parenting: character.rotation = m\_CharacterTargetRot; camera.localRotation = m\_CameraTargetRot; }
I'm sooo very glad I found this solution it saved my *ss :D I'm sharing it, so anyone who have similar problem might benefit.
Even though I'm an artist throughout the years spent in coding I started to see that the programming is actually the real art :) .
-
Thank you Eddy, yes I did it in a similar way you described, fortunately last night when I was scrolling down Google results(again....) I found this article: Preserving the players rotation on rotating platforms : Unity3D[^] The user there experienced the exact same problem as mine and he managed to solve it by changing the 'localRotation' to 'rotation' in the MouseLook.cs class from the standard Unity FPS controller, here is the code for the fix(this is the last if... statement from LookRotation void:
if (clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);if (smooth) { character.localRotation = Quaternion.Slerp(character.localRotation, m\_CharacterTargetRot, smoothTime \* Time.deltaTime); camera.localRotation = Quaternion.Slerp(camera.localRotation, m\_CameraTargetRot, smoothTime \* Time.deltaTime); } else { //comment the original rotation: //character.localRotation = m\_CharacterTargetRot; //put this line to fix the rotation when parenting: character.rotation = m\_CharacterTargetRot; camera.localRotation = m\_CameraTargetRot; }
I'm sooo very glad I found this solution it saved my *ss :D I'm sharing it, so anyone who have similar problem might benefit.
Even though I'm an artist throughout the years spent in coding I started to see that the programming is actually the real art :) .