Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使对象在距离它';s原始位置/旋转,而不是每次添加/删除新旋转?_C#_Unity3d - Fatal编程技术网

C# 如何使对象在距离它';s原始位置/旋转,而不是每次添加/删除新旋转?

C# 如何使对象在距离它';s原始位置/旋转,而不是每次添加/删除新旋转?,c#,unity3d,C#,Unity3d,它现在所做的是不断地向旋转中添加或删除一个随机数,因此在某些情况下,旋转可以是230、450或21,它在-70到70的范围内添加/删除,但我希望它添加/删除,但仅从原始开始位置/旋转中添加/删除70/-70 例如,如果游戏开始,且对象旋转为20,则添加/删除70,旋转将随机在20+70和20-70之间,最大值为90,最小值为-50。Rotate将相对于当前旋转进行操作,因此您实际上正在累积旋转。试试这个 using System.Collections; using System.Collect

它现在所做的是不断地向旋转中添加或删除一个随机数,因此在某些情况下,旋转可以是230、450或21,它在-70到70的范围内添加/删除,但我希望它添加/删除,但仅从原始开始位置/旋转中添加/删除70/-70


例如,如果游戏开始,且对象旋转为20,则添加/删除70,旋转将随机在20+70和20-70之间,最大值为90,最小值为-50。

Rotate
将相对于当前旋转进行操作,因此您实际上正在累积旋转。试试这个

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

public class Rotateturret : MonoBehaviour
{
    public enum RotateOnAxis { rotateOnX, rotateOnY, rotateOnZ };
    public bool randomRotation = false;
    [Range(0, 360)]
    public int rotationRange = 70;

    private void Start()
    {

    }

    private void Update()
    {
        if (randomRotation == true)
        {
            var rr = Random.Range(-rotationRange, rotationRange);
            transform.Rotate(Vector3.up, rr);
        }
    }
}

Rotate
将相对于当前旋转进行操作,因此实际上是在累积旋转。试试这个

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

public class Rotateturret : MonoBehaviour
{
    public enum RotateOnAxis { rotateOnX, rotateOnY, rotateOnZ };
    public bool randomRotation = false;
    [Range(0, 360)]
    public int rotationRange = 70;

    private void Start()
    {

    }

    private void Update()
    {
        if (randomRotation == true)
        {
            var rr = Random.Range(-rotationRange, rotationRange);
            transform.Rotate(Vector3.up, rr);
        }
    }
}
我相信使用一个“currentRotation”变量和一个“startRotation”变量并将随机值添加到其中会起作用。我在下面的部分将其添加到您现有的脚本中

Quaternion initialRotation;

void Start ()
{
    // Save initial rotation
    initialRotation = transform.rotation;
}

void Update ()
{
    // Compute random angle in the range of +- rotationRange
    var delta = (Random.value * 2f - 1f) * rotationRange;

    // Compute rotation from the initial rotation and delta
    transform.rotation = initialRotation * Quaternion.Euler(0f, delta, 0f);
}
我相信使用一个“currentRotation”变量和一个“startRotation”变量并将随机值添加到其中会起作用。我在下面的部分将其添加到您现有的脚本中

Quaternion initialRotation;

void Start ()
{
    // Save initial rotation
    initialRotation = transform.rotation;
}

void Update ()
{
    // Compute random angle in the range of +- rotationRange
    var delta = (Random.value * 2f - 1f) * rotationRange;

    // Compute rotation from the initial rotation and delta
    transform.rotation = initialRotation * Quaternion.Euler(0f, delta, 0f);
}