[英]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.