如何在一个轴上随机地在两个方向之间旋转对象? [英] How can I rotate an object from on one axis randomly between two directions?

查看:219
本文介绍了如何在一个轴上随机地在两个方向之间旋转对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造一种效果,就像对象环顾四周. 就像它在四处巡视.在这种情况下,他看着一个窗口,所以想法就是使他看起来像在外面看.

I want to create effect like the object is looking around. Like it's inspecting around. In this he is looking at a window so the idea is to make like he is looking the view outside.

这是Navi看着窗口的屏幕截图: 摄像头位于窗口之外,在Navi面上向前看:

This is a screenshot of the Navi looking at the window : The camera is positioned out of the window looking forward on the Navi face:

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

public class ItemAction : MonoBehaviour
{
    public float xAngle, yAngle, zAngle;
    public float speed;
    public camMouseLook mouselook;
    public GameObject lockedRoomCamera;
    public Camera playerCamera;
    public GameObject navi;
    private bool torotate = false;

    public void Init()
    {
        navi.transform.parent = null;
        navi.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        navi.transform.Rotate(new Vector3(0, 180, 0));
        PlayerController.disablePlayerController = true;
        mouselook.enabled = false;
        playerCamera.enabled = false;
        lockedRoomCamera.SetActive(true);
        torotate = true;

    }

    private void Update()
    {
        if(torotate == true)
        {
            navi.transform.Rotate(xAngle, Random.Range(90, 270) * speed * Time.deltaTime, zAngle, Space.Self);
        }
    }
}

我只想在y轴上随机地在90度到270度之间旋转对象.因此,看起来该对象在向左右两侧看.

I want to rotate the object only on the y axis randomly between 90 degrees and 270 degrees. So it will looks like the object is looking to the sides left and right.

但是现在对象只是在向左一个方向不停地旋转.

But now the object is just spinning nonstop on one direction to the left.

推荐答案

一种方法是使用协程这样频繁地生成随机旋转,然后随着时间的推移对其进行控制:

One way is to use a coroutine to generate random rotations every so often and then lerp to them over time:

IEnumerator DoLookAround() 
{
    float lookPeriod = 5f; // change look every 5 seconds
    float maxRotationSpeed = 90f; // turn no faster than 90 degrees per second
    Vector3 neutralForward = transform.forward;

    while(true) 
    {
        float timeToNextLook = lookPeriod;

        while (timeToNextLook > 0) {
            // Get random offset from forward
            float targetYRotation = Random.Range(-90f, 90f);

            // calculate target rotation
            Quaternion targetRotation = Quaternion.LookRotation(neutralForward, transform.up) 
                                       * Quaternion.AngleAxis(targetYRotation, Vector3.up);

            // rotate towards target limited by speed
            Quaternion newRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, maxRotationSpeed * Time.deltaTime);


            timeToNextLook -= Time.deltaTime;
            yield return null;
        }
    }
}

然后您可以通过以下方式调用它:

Then you can call it with:

StartCoroutine("DoLookAround");

并用

StopCoroutine("DoLookAround");

这篇关于如何在一个轴上随机地在两个方向之间旋转对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆