unity – How can I spin object randomly change direction every x seconds on the y axis only?


Even if I set the value 1 on the y only on the spinAxis variable and leave both x and z 0 it will spin also on the x and z.

How can I make the objectToSpin to spin only on the y axis ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MissleLauncherRotation : MonoBehaviour
{
    public GameObject objectToSpin;
    public Vector3 spinAxis;
    public float timeToSpin = 5f;
    public float spinSpeed = 20f;
    public bool randomSpin = false;

    private void Start()
    {
        StartCoroutine("Spin");
    }

    private void Update()
    {

    }

    IEnumerator Spin()
    {
        float spinTimer;
        while (true)
        {
            if (randomSpin == true)
            {
                spinAxis = Random.onUnitSphere;
            }

            spinTimer = timeToSpin;
            while (spinTimer > 0f)
            {
                objectToSpin.transform.Rotate(spinAxis, Time.deltaTime * spinSpeed);
                spinTimer -= Time.deltaTime;
                yield return null;
            }
        }
    }
}



Source link

More To Explore

Wylie Co VFX Reel 2021

The Californian studio Wylie Co. has just released their brand new showreel featuring breakdown footage from Dune and many more: © Vincent Frei – The

Share on facebook
Share on twitter
Share on linkedin
Share on email