使用浮点数制作旋转式可收藏品时出现错误。

huangapple go评论71阅读模式
英文:

Error in making a rotating collectible using a float

问题

我正在尝试为平台游戏制作一个3D旋转的可收集硬币,并且一直遇到这个错误,我不知道该如何修复。

错误信息:Assets/Scripts/Rotate.cs(18,26): error CS1503: 参数 1:无法从 'float' 转换为 'UnityEngine.Vector3'。

这是我的代码:

public class Rotate : MonoBehaviour
{
    [SerializeField] float speedY;

    void Update()
    {
        transform.Rotate(360 * speedY * Time.deltaTime);
    }
}

我尝试过使用公共的浮点数而不是SerializedField,但如预期的那样,没有产生任何效果。我期望它会以检视器中给定的速度进行旋转。

英文:

I am trying to make a rotating collectible coin in 3D for a platformer game, and I keep getting this error which I don't know how to fix.

Assets/Scripts/Rotate.cs(18,26): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector3'

Here is my code:

public class Rotate : MonoBehaviour
{
    [SerializeField] float speedY;

    void Update()
    {
        transform.Rotate(360 * speedY * Time.deltaTime);

    }
}

I've tried doing it with a public float instead of a SerializedField, but as expected, didn't do anything.
I'm expecting for it to rotate at the speed given in the inspector for the item.

答案1

得分: 1

你尚未指定旋转的轴线。
Transform.Rotate 接受 Vector3 作为参数,而你提供的是 float

你可以这样做:

transform.Rotate(360 * speedY * Vector3.up * Time.deltaTime);

或者更好地使用 Transform.Rotate(Vector3 axis, float angle) 重载:

transform.Rotate(Vector3.up, 360 * speedY * Time.deltaTime);
英文:

You haven't specified an axis around which you will rotate.
Transform.Rotate accepts Vector3 as an argument, and you provided float.

You could do:

transform.Rotate(360 * speedY * Vector3.up * Time.deltaTime);

or better use a Transform.Rotate(Vector3 axis, float angle) override:

transform.Rotate(Vector3.up, 360 * speedY * Time.deltaTime);

答案2

得分: 0

无法使用集合初始化程序来初始化类型 'type',因为它没有实现 'System.Collections.IEnumerable' 接口。要在类型上使用集合初始化程序,该类型必须实现 IEnumerable 接口。如果在想要使用对象初始化程序时使用集合初始化程序语法,可能会导致此错误。要纠正此错误,如果该类型不表示集合,请改用对象初始化程序语法,而不是集合初始化程序语法。如果该类型不表示集合,请修改它以实现 IEnumerable 接口。然后,您可以使用集合初始化程序来初始化该类型的对象。如果该类型表示集合,而您无法访问源代码,请使用它们的类构造函数或其他初始化方法来初始化其元素。

英文:

It is not possible to initialize a type 'type' with a collection initializer, because it does not implement 'System.Collections.IEnumerable' To use a collection initializer with a type, the type must implement IEnumerable. This error can occur if you are using collection initializer syntax when you want to use an object initializer. To correct this error If the type does not represent a collection, use object initializer syntax instead of collection initializer syntax. If the type does not represent a collection, modify it to implement IEnumerable . You can then use collection initializers to initialize objects of that type. If the type represents a collection and you don't have access to the source code, initialize its elements using their class constructors or other initialization methods.

huangapple
  • 本文由 发表于 2023年6月1日 06:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定