gpt4 book ai didi

c# - 如何检查旋转方向是否不为0且与上一个不相同?

转载 作者:行者123 更新时间:2023-11-30 23:03:22 24 4
gpt4 key购买 nike

不确定我是否在随机部分做对了,下一个方向不会是 0 并且不相同。

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

public class rotate : MonoBehaviour {

public Vector3 Direction;
[Range(0,300)]
public float speed = 10f;
public bool randomSpeed = false;
[Range(0.0f, 360.0f)]
public float angleToRotate = 360.0f;
public bool randomDirection = false;

private Quaternion lastRotation;
private Vector3 lastDirection;

// Use this for initialization
void Start ()
{
lastRotation = transform.rotation;
lastDirection = Direction;
}

private void Update()
{
if (randomSpeed)
{
speed = Random.Range(0, 300);
}

if (randomDirection)
{
Direction = new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));

if (Direction == new Vector3(0, 0, 0) || lastDirection == Direction)
{
Direction = new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));
}

lastDirection = Direction;
randomDirection = false;
}

transform.Rotate(Direction, speed * Time.deltaTime);
}
}

这部分:

 if (randomDirection)
{
Direction = new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));

if (Direction == new Vector3(0, 0, 0) || lastDirection == Direction)
{
Direction = new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));
}

lastDirection = Direction;
randomDirection = false;
}

我希望方向永远不会是 0,0,0,也不会与当前方向相同。所以每次下一个方向都会不同。

更新:

这就是我现在正在做的。 (尚未测试)

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

public class rotate : MonoBehaviour
{

public Vector3 Direction;
[Range(0, 300)]
public float speed = 10f;
public bool randomSpeed = false;
[Range(0.0f, 360.0f)]
public float angleToRotate = 360.0f;
public bool randomDirection = false;

private int counter = 0;

// Use this for initialization
void Start()
{

}

private void Update()
{
if (randomSpeed)
{
speed = Random.Range(0, 300);
}

RandomDirection();

transform.Rotate(Direction, speed * Time.deltaTime);
}

private void RandomDirection()
{
if (randomDirection)
{
Direction = new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));
while (Direction == new Vector3(0, 0, 0))
{
counter++;
Direction = new Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));

if (counter == 5)
{
Direction = new Vector3(1, 0, 0);
break;
}
}

counter = 0;
}
}
}

在 while 内的 RandomDirection 方法中,如果结果为 0,0,0 并且连续出现 5 次,那么我会给出一个默认方向并停止循环。

最佳答案

如果当前方向等于(0,0,0)或前一个方向。您可以通过我在下面编写的这个 ChangeToSlightlyDifferentVector3(Vector3 vector3) 方法运行方向。

它将通过加 1 或减 1 来偏移 vector3 中的每个坐标,但将其保持在 -1 到 1 的范围内。加或减 1 是随机的,但因为它会为全部或全部减一,并且在检查 (-1, -1, -1) 和 (1, 1, 1) 时,它不能以 (0,0,0) 或前一个方向结束。

Vector3 ChangeToSlightlyDifferentVector3(Vector3 vector3){
int RandomChance = Random.Range(0,100);
if (vector3.x == 1f && vector3.x == 1f && vector3.x == 1f)
{ //Has to "add or loop" so it doesn't end at 0.
RandomChance = 100f
}
else if (vector3.x == -1f && vector3.x == -1f && vector3.x == -1f)
{ //Has to "subtract or loop" so it doesn't end at 0.
RandomChance = 0f;
}
float x = RandomizeToDifferentOne(vector3.x, RandomChance);
float y = RandomizeToDifferentOne(vector3.y, RandomChance);
float z = RandomizeToDifferentOne(vector3.z, RandomChance);
Vector3 newVector3 = new Vector3(x,y,z);
return newVector3;
}

float RandomizeToDifferentOne(int input, int RandomizeChance){

if (RandomChance < 50)
{
return DeductOrLoop(input);
}
else
{
return AddOrLoop(input);
}
}

float AddOrLoop(float input){
float offset = Random.Range(0.1f, 0.75f);
float leftOver = (input + offset) - 1f;
input += offset;
if (input > 1f)
{
input = -1f + leftOver;
}

return input;
}

float DeductOrLoop(float input){
float offset = Random.Range(0.1f, 0.75f);
float leftOver = (input - offset) + 1f;
input -= offset;
if (input < -1f)
{
input = 1f;
input -= leftOver;
}
return input;
}

关于c# - 如何检查旋转方向是否不为0且与上一个不相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49966742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com