【问题标题】:How can I rotate an object with random speed with some delays?如何以随机速度旋转物体并有一些延迟?
【发布时间】:2020-05-03 13:22:30
【问题描述】:
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);
        }
    }
}

这是随机部分:

Random.Range(3,300)

但是因为它改变随机数的速度太快了,所以几乎看不到变化。 我想以某种方式做到这一点,例如,如果下一个随机数将以这个速度数字保持 5 秒,然后移动到下一个随机数并再次保持这个数字 5 秒,依此类推。

试过这个:

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
        }
    }
}

但这只是每 3 秒旋转一次对象。而不是一直旋转它,速度每 3 秒改变一次。

【问题讨论】:

  • 我通常是桌面或 Web 开发人员,但是当我需要使用 c# 延迟我的应用程序中的任何进程时,我使用 System.Threading.Thread.Sleep(),docs.microsoft.com/en-us/dotnet/api/…
  • 不要在 Unity 中使用类似的线程。使用协程。

标签: c# unity3d


【解决方案1】:

您有多种延迟随机化的方法。您可以添加 Time.deltaTime 直到达到特定的时间,或者您可以使用 InvokeRepeating 并调用您的 if-case。 https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

如果您需要更多帮助或代码,请直说。

编辑: 如果游戏对象需要以恒定速度旋转,请尝试使用 Rigidbody.addtorque https://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html 但因为它是一种强制,请记住在设置新值之前将其重置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多