Trying to make a fan

So I created this fan but its not spinning the way I want.
The script attached has the following running in it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
public class Motor : MonoBehaviour
{
[SerializeField] float speed = 2f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    transform.Rotate(Vector3.right * speed * Time.deltaTime);
}

}

How do I make it stay in place and just spin?

Vector3.right will make your fan spin on the X axis. that’s why it’s rotating that way.

For it to spin in place you’ll have to rotate it on the Y axis, for that use Vector3.up.

It’s still not working, when I apply the rotation script to any simple game object, the rotation is working as intended but when I use it on a compound object like the fan I created the rotation becomes very weird like in the gif I uploaded.

Be aware of the pivot point. As seen in the example below, the green dot represents the pivot point, when the 3 stretched cubes are right in the middle of it they rotate while staying in place, when moved away from the point you’ll see that now they move around it.

ezgif-5-4f4b59a73194

I’m using the exact same code as yours.

There’s also something I noticed from your gif, your starting rotation is not 0,0,0, but -0.516, 33.8, 88.18… that will make your object behave weirdly when trying to rotate it. When dealing with rotations a good practice is to child the objects you want to rotate to an empty object with scale 1,1,1 otherwise it may cause some scale deformations, and rotation 0,0,0 so you can precisely know how to rotate the object.

If you imported your fan from Blender you’ll also have to take into consideration that Blender uses a right hand coordinate system while Unity uses a left hand coordinate system, that means your objects will be rotated right from the start which might not be a good idea.

1 Like

Here’s a little script I wrote for causing things to spin. Just drag it into your fan object and you should be good. It’ll add an attribute in the inspector for the axis to spin around, and another one for speed (RPMs). Good luck!

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class spinner : MonoBehaviour

{

    public Vector3 axis = new Vector3(0,0,1);

    private Vector3 axis_start;

    public float RPM = 1f;

    // Start is called before the first frame update

    void Start()

    {

        axis_start = transform.rotation.eulerAngles;

    }

    // Update is called once per frame

    void Update()

    {

        transform.Rotate(axis, RPM * 6f * Time.deltaTime);

    }

}
2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms