本文介绍了将数字除以 Vector3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将一个数字除以 Vector3?例如,如何将 1 除以对象的比例向量以根据其父比例调整其子项的大小,而无需分别应用每个轴的值来创建新的 Vector3?

Is it possible to divide a number by a Vector3? For example, how can I divide 1 by the scale vector of an object to resize it's child according to it's parent scale without making a new Vector3 applying values of each axis respectivly?

推荐答案

没有内置这种方法

但是你可以简单地添加一个扩展方法一次喜欢

but you can simply add an extension method once like

public static class Vector3Extensions
{
    /// <summary>
    /// Inverts a scale vector by dividing 1 by each component
    /// </summary>
    public static Vector3 Invert(this Vector3 vec)
    {
        return new Vector3(1 / vec.x, 1 / vec.y, 1 / vec.z);
    }
}

然后在你所有的脚本中你只需要做例如

than later in all your scripts you just have to do e.g.

var parentSize = transform.parent.lossyScale; // e.g. 1, 2, 3

var invertedParentSize = parentSize.Invert();

// -> 1.0, 0.5, 0.333..

这篇关于将数字除以 Vector3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:47