Search code examples
c#unity-game-engine3d

Error in making a rotating collectible using a float


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.


Solution

  • 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);