0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Turn_Move : MonoBehaviour {
    public int TurnX;
    public int TurnY;
    public int TurnZ;

    public int MoveX;
    public int MoveY;
    public int MoveZ;

    public bool World;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (World == true) {
            transform.Rotate(TurnX * Time.deltaTime,TurnY * Time.deltaTime,TurnZ * Time.deltaTime, Space.World);
            transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
        }else{
            transform.Rotate(TurnX * Time.deltaTime,Random.Range(3,300) * Time.deltaTime,TurnZ * Time.deltaTime, Space.Self);
            transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
        }
    }
}

This is the random part :

Random.Range(3,300)

but sine it's changing the random numbers too quick the changes are almost not visible. I want somehow to make that if for example the next random will stay for example 5 seconds at this speed number then move to the next random number and again stay at this number 5 seconds and so on.

Tried this :

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

public class Turn_Move : MonoBehaviour {
    public int TurnX;
    public int TurnY;
    public int TurnZ;

    public int MoveX;
    public int MoveY;
    public int MoveZ;

    public bool World;

    private bool IsGameRunning = false;

    // Use this for initialization
    void Start () 
    {
        IsGameRunning = true;
        StartCoroutine(SpeedWaitForSeconds());
    }

    // Update is called once per frame
    void Update () {

    }

    IEnumerator SpeedWaitForSeconds()
    {
        var delay = new WaitForSeconds(3);//define ONCE to avoid memory leak
        while (IsGameRunning)
        {
            if (World == true)
            {
                transform.Rotate(TurnX * Time.deltaTime, TurnY * Time.deltaTime, TurnZ * Time.deltaTime, Space.World);
                transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
            }
            else
            {
                transform.Rotate(TurnX * Time.deltaTime, Random.Range(3, 300) * Time.deltaTime, TurnZ * Time.deltaTime, Space.Self);
                transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
            }

            yield return delay;//wait
        }
    }
}

but that just rotate the object once each 3 seconds. and not rotating it all the time with speed changing every 3 seconds.

but now it's not rotating for 3 seconds it rotating every 3 seconds.

better to describe what I want is like intervals. The object should spin all the time nonstop like i Update but then each 3 seconds to change for a random speed.

for example spin at speed 5 ....after 3 seconds spin at speed 77 ....after 3 second spin at speed 199

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
public class Turn_Move : MonoBehaviour {
public int TurnX;
public int TurnY;
public int TurnZ;

public int MoveX;
public int MoveY;
public int MoveZ;

public bool World;

int randomNumber;

// Use this for initialization
void Start () {
    InvokeRepeating("GetRandomNumber",0,5);
}

// Update is called once per frame
void Update () {
    if (World == true) {
        transform.Rotate(TurnX * Time.deltaTime,TurnY * Time.deltaTime,TurnZ * Time.deltaTime, Space.World);
        transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.World);
    }else{
        transform.Rotate(TurnX * Time.deltaTime, randomNumber * Time.deltaTime,TurnZ * Time.deltaTime, Space.Self);
        transform.Translate(MoveX * Time.deltaTime, MoveY * Time.deltaTime, MoveZ * Time.deltaTime, Space.Self);
    }
}

void GetRandomNumber()
{randomNumber = Random.Range(3,300);}}

You can try this.

\$\endgroup\$
5
  • \$\begingroup\$ Could you please explain your solution? \$\endgroup\$
    – lvictorino
    May 4, 2020 at 6:10
  • \$\begingroup\$ Is it what you are looking for? or I misunderstood the question? \$\endgroup\$
    – Navdeep
    May 4, 2020 at 12:53
  • \$\begingroup\$ I'm not the one who asked. I just think this is a site where people come to learn and improve their knowledge. Therefore copy/paste some code without any comment or explanation is not helpful in that sense. \$\endgroup\$
    – lvictorino
    May 4, 2020 at 12:55
  • \$\begingroup\$ I didn't copy past the question. What happening was Random.Range(3,300) was called in every frame, but what he want - a random number that will stay for 5 sec(lets assume). So I am changing the random number by using "InvokeRepeating("GetRandomNumber",0,5);" this will change the number after 5 sec. \$\endgroup\$
    – Navdeep
    May 4, 2020 at 12:59
  • \$\begingroup\$ Thanks, it's just what I asked for: some explanation for the poster to understand his/her mistake. Thanks for them. \$\endgroup\$
    – lvictorino
    May 4, 2020 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .