简体   繁体   中英

Building a rotating platform in Unity

I got a plattform which rotates around the axis and keeps the player rotating with it.

At first, I made a platform rotate around the y-axis

[SerializeField]
bool counterclockwise; // rotation left or right?

[SerializeField]
private int speed;

private void Start()
{
    if (counterclockwise)
        speed = -speed;
}

private void Update()
{
    transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));
}

private void OnTriggerEnter(Collider col)
{
    col.transform.parent = transform; // keep the player as a child, to rotate him with the platform rotation
}

private void OnTriggerExit(Collider col)
{
    col.transform.parent = null; // remove the player as a child
}

and this works really awesome. So when trying to rotate the platform on the x-axis or z-axis, some errors appear.

When the player gets a child of the platform (while rotating on the x-axis or z-axis) he grows really big, the physics crashes, he automatically flies into the air with no gravity, ...

Can someone help me solving this bug?

So the current code is

[SerializeField]
Vector3 rotationVector;

private void Update()
{
    transform.Rotate(rotationVector * Time.deltaTime);
}

private void OnTriggerEnter(Collider col)
{
    col.transform.parent = transform;
}

private void OnTriggerExit(Collider col)
{
    col.transform.parent = null;
}

Apparently the problem happens when you setParent to GameObject, nothing to do with the triger enter or the rotation, and the cause for this problem seems to be a combination of:

  • non uniform scaling on your parent object.
  • AND arbitrary rotation on your child object

I made several experimentes with both:

character.transform.parent = this.transform;

and

character.transform.SetParent(this.transform);

And the issue persist.

I solved the problem by creating a Empty GameObject , placed it in the middle of the platform (you can program this or use the editor). Then I createed a script to make both, platform and character, children of this Empty GameObject. It is necessary to perform the rotations also in the update() of this Empty GameObject.

You can try to write this script adding just a Start() and Update() method, to test this solution:

void Start () {

    GameObject character = GameObject.FindGameObjectWithTag("character");
    GameObject platform = GameObject.FindGameObjectWithTag("platform");

    character.transform.SetParent(this.transform,false);
    platform.transform.SetParent(this.transform,false);

}

And for the Update()

  private void Update()
    {
        transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));

        //transform.Rotate(new Vector3(speed * Time.deltaTime, 0,  0));

        //transform.Rotate(new Vector3(0 , 0 , speed * Time.deltaTime));
    }

After testing, you will still need to implement the part of OnTriggerEnter and other things, but at least you will see with the Empty GameObject you can "link both"

I guess if for one of the rotation you didnt see any issue is because the deformation is hidden for that angle, but probaly if you turn the GameObject you will see it, and will be hidden for the rotation in another angle. It will depend on the shape of your player and platform GameObjects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM