简体   繁体   English

C#运算符'/'不能应用于类型为'Vector3'和'float'的操作数

[英]C# Operator '/' cannot be applied to operands of type 'Vector3' and 'float'

I'm smoothing a Vector 3 angle, but right now I take in my smooth a vector 3 angle. 我正在平滑矢量3的角度,但是现在我将平滑矢量3的角度。 Right now the smoothing factor is 0-1 with 1 being no smooth. 现在,平滑因子是0-1,其中1表示不平滑。 But I would like to have the smoothing factor 0-100 with 100 being no smooth. 但是我想使平滑系数为0-100,而100不平滑。 So I came up with this hoping it would work. 所以我想到了这个希望。

        public static Vector3 SmoothAngle(this Vector3 src, Vector3 dest, float smoothAmount)
    {
        Vector3 SmoothedAngle;
        SmoothedAngle = dest - src;
        SmoothedAngle = ClampAngle(SmoothedAngle);
        SmoothedAngle = src + SmoothedAngle / 100f * (100f - smoothAmount);
        Console.WriteLine(SmoothedAngle);
        return ClampAngle(SmoothedAngle);
    }

But I get the error as stated in my title. 但是我得到了标题中所述的错误。 This is my vector 3 class. 这是我的向量3类。

public struct Vector3
{
    #region VARIABLES
    public float X;
    public float Y;
    public float Z;
    #endregion

    #region PROPERTIES
    public static Vector3 Zero => new Vector3(0, 0, 0);
    public static Vector3 UnitX => new Vector3(1, 0, 0);
    public static Vector3 UnitY => new Vector3(0, 1, 0);
    public static Vector3 UnitZ => new Vector3(0, 0, 1);

    #endregion

    #region CONSTRUCTOR
    public Vector3(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }
    public Vector3(Vector3 vec) : this(vec.X, vec.Y, vec.Z) { }
    public Vector3(float[] values) : this(values[0], values[1], values[2]) { }
    #endregion

    #region METHODS
    public float Length()
    {
        return (float)Math.Abs(Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2) + Math.Pow(Z, 2)));
    }
    public float DistanceTo(Vector3 other)
    {
        return (this - other).Length();
    }

    public override bool Equals(object obj)
    {
        Vector3 vec = (Vector3)obj;
        return GetHashCode() == vec.GetHashCode();
    }

    public override int GetHashCode()
    {
        return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
    }

    public override string ToString()
    {
        return $"[X={X.ToString()}, Y={Y.ToString()}, Z={Z.ToString()}]";
    }
    #endregion

    #region OPERATORS
    public static Vector3 operator +(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
    }
    public static Vector3 operator -(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);
    }
    public static Vector3 operator *(Vector3 v1, float scalar)
    {
        return new Vector3(v1.X * scalar, v1.Y * scalar, v1.Z * scalar);
    }
    public static bool operator ==(Vector3 v1, Vector3 v2)
    {
        return v1.X == v2.X && v1.Y == v2.Y && v1.Z == v2.Z;
    }
    public static bool operator !=(Vector3 v1, Vector3 v2)
    {
        return !(v1 == v2);
    }
    public float this[int i]
    {
        get
        {
            switch (i)
            {
                case 0:
                    return X;
                case 1:
                    return Y;
                case 2:
                    return Z;
                default:
                    throw new IndexOutOfRangeException();
            }
        }
        set
        {
            switch (i)
            {
                case 0:
                    X = value;
                    break;
                case 1:
                    Y = value;
                    break;
                case 2:
                    Z = value;
                    break;
                default:
                    throw new IndexOutOfRangeException();
            }
        }
    }
    #endregion
}

You've implemented the * operator, but not the / operator. 您已经实现了*运算符,但没有实现/运算符。

public static Vector3 operator /(Vector3 v1, float scalar)
{
    return new Vector3(v1.X / scalar, v1.Y / scalar, v1.Z / scalar);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符“*”不能应用于“Vector3”和“Vector3”类型的操作数 - Operator '*' cannot be applied to operands of type 'Vector3' and 'Vector3' 如果 c# 错误提示“运算符'*'不能应用于'Vector3'和'Vector3'类型的操作数”,我该怎么办? - what do i do if a c# error says " operator '*' cannot be applied to operands of the type 'Vector3' and 'Vector3' " Unity运算符`-=&#39;不能应用于&#39;float&#39;和&#39;UnityEngine.Vector3&#39;类型的操作数 - Unity Operator `-=' cannot be applied to operands of type `float' and `UnityEngine.Vector3' 运算符“-”不能应用于“ float”和“ string”类型的操作数 - Operator '-' cannot be applied to operands of type 'float' and 'string' 运算符“*”不能应用于“void”和“float”类型的操作数 - Operator '*' cannot be applied to operands of type 'void' and 'float' C#:运算符&#39;!=&#39;不能应用于类型&#39;object&#39;和&#39;char&#39;的操作数 - C#: Operator '!=' cannot be applied to operands of type 'object' and 'char' C#运算符&#39;+&#39;不能应用于类型&#39;IntPtr&#39;和&#39;int&#39;的操作数 - C# Operator '+' cannot be applied to operands of type 'IntPtr' and 'int' C#中的比较:operator&#39;&lt;&#39;不能应用于&#39;T&#39;和&#39;T&#39;类型的操作数 - Comparison in C#: operator '<' cannot be applied to operands of type 'T' and 'T' C#错误:operator&#39;!=&#39;不能应用于&#39;char&#39;和&#39;string&#39;类型的操作数 - C# error: operator '!=' cannot be applied to operands of type 'char' and 'string' C#运算符&#39;/&#39;不能应用于&#39;方法组&#39;和&#39;int类型的操作数 - C# Operator '/' cannot be applied to operands of type 'method group' and 'int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM