简体   繁体   English

为什么即使我在特定度数范围之间随机限制旋转,变换也会旋转 360 度?

[英]Why the transform is rotating 360 degrees even if I limit the rotation randomly between specific degrees ranges?

The guns is rotating 360 degrees and not between -40 and 0枪正在旋转 360 度,而不是在 -40 和 0 之间

This screenshot is the default before running the game the rotation on x is 0 :此屏幕截图是运行游戏前的默认设置,x 上的旋转为 0 :

枪 x 旋转值为 0

and this screenshot is after I changed the guns x rotation value to -40这个截图是在我将枪械 x 旋转值更改为 -40 之后

枪 x 旋转值为 0

I want the guns to rotate up down between 0 and -40 but when I'm running the game the guns rotating 360 degrees.我希望枪在 0 到 -40 之间向上旋转,但是当我运行游戏时,枪旋转了 360 度。

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

public class MissleLauncherRotation : MonoBehaviour
{
    public Transform body;
    public Transform guns;
    public Vector3 spinAxisBody;
    public Vector3 spinAxisGuns;
    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)
            {
                spinAxisBody = new Vector3(0,Random.Range(-180, 180),0);

                spinAxisGuns = new Vector3(Random.Range(-40, 0), 0, 0);
            }

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

I tried this :我试过这个:

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

public class MissleLauncherRotation : MonoBehaviour
{
    public Transform body;
    public Transform guns;
    public Vector3 spinAxisBody;
    public Vector3 spinAxisGuns;
    public float timeToSpin = 5f;
    public float spinSpeed = 20f;
    public bool randomSpin = false;

    Quaternion rot;

    private void Start()
    {
        rot = guns.rotation;
    }

    private void Update()
    {
        guns.Rotate(guns.up, (spinSpeed * Time.deltaTime));
        var rot = guns.rotation.eulerAngles;
        rot = new Vector3
                                        (
                                            guns.eulerAngles.x,
                                            Mathf.Clamp(rot.x, -40, 0),
                                            guns.eulerAngles.z
                                        );
        guns.rotation = Quaternion.Euler(rot);
    }
}

The only thing is working now is the clamping but the clamping is not only on the x and I want to use coroutine or somehow to make that it will rotate ever x seconds.现在唯一起作用的是夹紧,但夹紧不仅在 x 上,我想使用协程或以某种方式使其每 x 秒旋转一次。

I did it this way to test the clamping if it's working at all now how do I use it with my first code ?我这样做是为了测试夹紧是否正常工作,现在我如何将它与我的第一个代码一起使用?

As already mentioned Rotate adds a rotation to the current one.如前所述, Rotate向当前Rotate添加了一个旋转。

I would rather precalculate the target rotation and use eg我宁愿预先计算目标旋转并使用例如

public Transform body;
public Transform guns;
public Vector3 spinAxisBody;
public Vector3 spinAxisGuns;
public float timeToSpin = 5f;
public float spinSpeed = 20f;
public bool randomSpin = false;

IEnumerator Start()
{
    while (true)
    {
        if (randomSpin == true)
        {
            spinAxisBody = new Vector3(0,Random.Range(-180, 180),0);

            spinAxisGuns = new Vector3(Random.Range(-40, 0), 0, 0);
        }

        var startBodyRotation = body.localRotation;
        var targetBodyRotation = startBodyRotation * Quaternion.Euler(spinAxisBody);   

        var startGunRotation = guns.localRotation;
        var targetGunsRotation = startGunRotation * Quaternion.Euler(spinAxisGuns);  

        for(spinTimer = 0; spinTimer < timeToSpin; spinTimer += Time.deltaTime)
        {
            // Factor linear moving from 0 to 1 within timeToSpin seconds
            var factor = spinTimer / timeToSpin;
            // optional add ease-in and -out
            //factor = Mathf.SmoothStep(0, 1, factor);

            body.transform.localRotation = Quaternion.Lerp(startBodyRotation, targetBodyRotation, factor);
            guns.localRotation(startGunRotation, targetGunsRotation, factor);

            yield return null;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM