Unity 有损缩放实现 [英] Unity Lossy Scaling implementation

查看:57
本文介绍了Unity 有损缩放实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Unity 和 transform.rotation.问题是我有 2 个旋转对象作为父子对象.这使孩子奇怪地缩放并从盒子变成平行四边形.Gooing 我发现 transform.lossyScale 应该可以解决我的问题,但我不知道如何实现它.

I am currently working with Unity and transform.rotation. The problem is that I have 2 rotating objects as parent-child. This makes the child scale weirdly and going from a box to a paralelogram. Gooing I found transform.lossyScale that should fix my problem but I don´t know how to implement it.

TL:DR 如何实现 transform.lossyscale 以防止父子缩放问题.

TL:DR how to implement transform.lossyscale to prevent parent-child scaling issues.

当前代码

float currentR = transform.eulerAngles.x;

    if(currentR < 30){
        transform.Rotate (rotation * Time.deltaTime * 3);
}

推荐答案

理想情况下,如果您不希望孩子继承父级的规模,请不要将其设为所述父级的子级 :P然而.如果必须,您可以创建一个与父级相反的子级(因此它将具有与父级相反的级)

Ideally, if you don't want the child to inheirt the scale of the parent, don't make it a child of said parent :P However. If you must, you can make a child that reverses the scale of the parent (So it would have the opposite scale of the parent)

比让你的新孩子成为那个孩子的孩子:)

Than make your new child a child of that child :)

你可以使用这个通用脚本,把它放在子节点上,即使它有一个缩放的父节点,它在运行时也不应该有任何旋转问题:)(它将使用代码完成我上面描述的事情)

You can use this general purpose script, put this on the child at it shouldn't have any issues being rotated at runtime even if it has a scaled parent :) (It will do the thing I described above using code)

public class IgnoreParentScale : MonoBehaviour {

Transform scaleInversionParent;

void Awake () {
    scaleInversionParent = new GameObject ("ScaleInversionParent").transform;
    scaleInversionParent.SetParent (transform.parent);
    scaleInversionParent.localPosition = Vector3.zero;
    scaleInversionParent.eulerAngles = Vector3.zero;//Zero out anything but the scale
    UpdateScaleInversion ();
    transform.SetParent (scaleInversionParent, true);
}
void LateUpdate () {
    UpdateScaleInversion (); //Only if you are changing the scale at run time.
}
void UpdateScaleInversion () {
    Vector3 invertedScale = scaleInversionParent.parent.localScale;
    invertedScale = new Vector3 (1f / invertedScale.x, 1f / invertedScale.y, 1f / invertedScale.z);
    scaleInversionParent.localScale = invertedScale;//This scale will invert the parents scale
}
}

或者:

您可以使用此脚本创建一个新的父级,该父级继承原始父级的位置和旋转,但忽略其缩放.

You can use this script to create a new parent that inherits the position and rotation of the original parent, but ignores it's scale.

    public class IgnoreParentScale : MonoBehaviour {

    Transform originalParent;
    Transform newParent;

    void Awake () {
        newParent = new GameObject ("").transform;
        originalParent = transform.parent;

        UpdateNewParent ();
        transform.SetParent (newParent);
    }
    public void LateUpdate () {
        UpdateNewParent ();
    }
    void UpdateNewParent () {
        newParent.transform.position = originalParent.transform.position;
        newParent.transform.rotation = originalParent.transform.rotation;
    }
}

这篇关于Unity 有损缩放实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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