How can i rotate a 3D platform and fix a little problem?...
I am using the First Person Controller script from Unity. If i jump on a platform i use this script to hold the character on a platform, so he is moving with it:
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
other.transform.parent = gameObject.transform;
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == "Player")
{
other.transform.parent = null;
}
}
The problem with that is... i also get the same rotation of that object(platform) and this makes the problem. If i jump on it or from it away. I get a whole different rotation on my character and it makes it unplayable.
To rotate the platform i use this script, both scripts are on the rotating platform:
float y;
public float speedY;
void FixedUpdate ()
{
y += Time.deltaTime * speedY;
transform.rotation = Quaternion.Euler(0,y,0);
}
Of course the player is rotating with the platform. It depends on what you want to achieve. Try to explain what should happen when the player is on a platform and what happens when gets off it?
I want if the player jumps on the platform, he stays with his own rotation he entered first (not changing to a complete new direction of rotation of that parent object), but if he stay on the rotating platform, he should then rotate from his local rotation with the platform together. If i jump off that platform, i want to stick with the new rotation the player had from that platform, so there are no fast camera rotation changes.
At the moment, if i jump on the platform, the player rotation change completely and if i jump off the platform, it also change again in his old local rotation (the same rotation he entered the platform first).
The code in OnTriggerEnter should do what you wanted then.
For OnTriggerExit to work you'd need to save the players rotation in OnTriggerEnter and reset it after you've set the parent to null.
Answer by AMU4u · Mar 04, 2017 at 07:24 PM
public bool onObject;
public bool freeFromObject;
void OnTriggerEnter()
{
freeFromObject = false;
onObject = true;
}
void OntriggerExit()
{
onObject = false;
freeFromObject = true;
void IfFreeOfObjects()
{
Transform.Rotate(5*speed*Time.DeltaTime,0,0);
}
void IfOnObject()
{
Transform.Rotate(objectIWantToRotateWith);
}
Void FixedUpdate()
{
if(onObject)
{
IfOnObject();
}
else if(freeFromObject)
{
IfFreeFromObject();
}
}
@letitslide I tried your script to add it on the rotating platform, but it doesnt work. $$anonymous$$aybe there is something missing in that script?
Well, it's psuedo-code that closely resembles real code. It's lacking a starting bracket on the class, the using UnityEngine and such at the start of the class. Just paste the values into a script that you are using that works, and resolve the syntax errors from there.
error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.Rotate(float, float, float)'
If i add the platform object and put it there, i get this message?
void IfOnObject()
{
Transform.Rotate(platformObj);
}
Answer by reigota · Mar 13, 2017 at 01:54 AM
As @hexagonius said, use OntriggerEnter to store original player rotation and OnTriggerExit to restore it. But, to set the parente, use method .SetParent of the transform. It has a second parameter that lets you to keep original world position/rotation/scale while parenting..
Something like:
Quaternion _playerOriginalRotation;
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "Player")
{
_playerOriginalRotation = other.transform.rotation;
other.transform.SetParent( gameObject.transform, true);
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.tag == "Player")
{
other.transform.parent = null;
other.transform.rotation = _playerOriginalRotation;
}
}
Hi @reigota thank you for helping, i tried this script of you, but i still got the same problem. I added it to the rotating platform, then the player with first person controller script (from Unity), is jumping on it and then i got the same rotation from that rotating object and the camera change each time i jump on it. The rotating platform problem seems to be not easy to solve. 109 peoples follow this question too. You have any idea why it still doesnt work?