不知道我是否在随机部分正确地执行了下一个方向将不为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,也不会与当前方向相同。因此,每次下一个方向都会有所不同。
相关分类