在Unity中构建旋转平台 [英] Building a rotating platform in Unity

查看:45
本文介绍了在Unity中构建旋转平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个平台可以绕轴旋转并让玩家随之旋转.

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

首先,我让一个平台绕 y 轴旋转

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
}

这真的很棒.所以当试图在 x 轴或 z 轴上旋转平台时,会出现一些错误.

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

当玩家得到平台的孩子时(同时在 x 轴或 z 轴上旋转)他变得非常大,物理崩溃,他自动飞入空中,没有重力,......

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?

所以当前的代码是

[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;
}

推荐答案

显然是你setParent到GameObject时出现的问题,与triger enter或rotation无关,导致这个问题的原因似乎是:

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:

  • 父对象上的非统一缩放.
  • AND 任意旋转您的子对象

我对两者进行了多次实验:

I made several experimentes with both:

character.transform.parent = this.transform;

character.transform.SetParent(this.transform);

并且问题仍然存在.

我通过创建一个空游戏对象解决了这个问题,并将其放置在平台的中间(您可以对其进行编程或使用编辑器).然后我创建了一个脚本来制作这个 Empty GameObject 的平台和角色的孩子.也有必要在这个 Empty GameObject 的 update() 中执行旋转.

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.

您可以尝试编写此脚本,仅添加一个 Start() 和 Update() 方法,以测试此解决方案:

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);

}

对于 Update()

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));
    }

经过测试,您仍然需要实现 OnTriggerEnter 等部分,但至少您会看到使用 Empty GameObject 可以链接两者"

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"

我想如果对于其中一个旋转,您没有看到任何问题是因为该角度的变形是隐藏的,但可能如果您转动 GameObject,您会看到它,并且会在另一个角度的旋转中隐藏.这将取决于您的玩家和平台游戏对象的形状

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

这篇关于在Unity中构建旋转平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆