圆内随机移动
random movement within a circle
所以我的问题是我想让我的物体以一定的速度在一定的范围内随机移动。
现在它移动得非常快,乘以速度是行不通的。
这是我的代码:
using UnityEngine;
using System.Collections;
public class RandomTargetMovement : MonoBehaviour {
public float radius = 20.0f;
void Update(){
transform.position = Random.insideUnitCircle * radius;
}
}
那是因为您在每次更新时都会生成一个新的随机数。这有几个原因很糟糕。
但在这个特定的例子中,它不仅不好,而且根本不起作用。这是因为每次渲染帧时都会调用 update,这意味着无论您如何设置速度,您总会有不平稳的运动。为此,您应该使用 deltaTime.
我假设你想让物体移动到一个点,然后开始向一个新的随机点移动。这是一个不太优雅的解决方案:
using UnityEngine;
using System.Collections;
public class TestSample : MonoBehaviour {
public float radius = 40.0f;
public float speed = 5.0f;
// The point we are going around in circles
private Vector2 basestartpoint;
// Destination of our current move
private Vector2 destination;
// Start of our current move
private Vector2 start;
// Current move's progress
private float progress = 0.0f;
// Use this for initialization
void Start () {
start = transform.localPosition;
basestartpoint = transform.localPosition;
progress = 0.0f;
PickNewRandomDestination();
}
// Update is called once per frame
void Update () {
bool reached = false;
// Update our progress to our destination
progress += speed * Time.deltaTime;
// Check for the case when we overshoot or reach our destination
if (progress >= 1.0f)
{
progress = 1.0f;
reached = true;
}
// Update out position based on our start postion, destination and progress.
transform.localPosition = (destination * progress) + start * (1 - progress);
// If we have reached the destination, set it as the new start and pick a new random point. Reset the progress
if (reached)
{
start = destination;
PickNewRandomDestination();
progress = 0.0f;
}
}
void PickNewRandomDestination()
{
// We add basestartpoint to the mix so that is doesn't go around a circle in the middle of the scene.
destination = Random.insideUnitCircle * radius + basestartpoint;
}
}
希望对您有所帮助。
所以我的问题是我想让我的物体以一定的速度在一定的范围内随机移动。 现在它移动得非常快,乘以速度是行不通的。
这是我的代码:
using UnityEngine;
using System.Collections;
public class RandomTargetMovement : MonoBehaviour {
public float radius = 20.0f;
void Update(){
transform.position = Random.insideUnitCircle * radius;
}
}
那是因为您在每次更新时都会生成一个新的随机数。这有几个原因很糟糕。
但在这个特定的例子中,它不仅不好,而且根本不起作用。这是因为每次渲染帧时都会调用 update,这意味着无论您如何设置速度,您总会有不平稳的运动。为此,您应该使用 deltaTime.
我假设你想让物体移动到一个点,然后开始向一个新的随机点移动。这是一个不太优雅的解决方案:
using UnityEngine;
using System.Collections;
public class TestSample : MonoBehaviour {
public float radius = 40.0f;
public float speed = 5.0f;
// The point we are going around in circles
private Vector2 basestartpoint;
// Destination of our current move
private Vector2 destination;
// Start of our current move
private Vector2 start;
// Current move's progress
private float progress = 0.0f;
// Use this for initialization
void Start () {
start = transform.localPosition;
basestartpoint = transform.localPosition;
progress = 0.0f;
PickNewRandomDestination();
}
// Update is called once per frame
void Update () {
bool reached = false;
// Update our progress to our destination
progress += speed * Time.deltaTime;
// Check for the case when we overshoot or reach our destination
if (progress >= 1.0f)
{
progress = 1.0f;
reached = true;
}
// Update out position based on our start postion, destination and progress.
transform.localPosition = (destination * progress) + start * (1 - progress);
// If we have reached the destination, set it as the new start and pick a new random point. Reset the progress
if (reached)
{
start = destination;
PickNewRandomDestination();
progress = 0.0f;
}
}
void PickNewRandomDestination()
{
// We add basestartpoint to the mix so that is doesn't go around a circle in the middle of the scene.
destination = Random.insideUnitCircle * radius + basestartpoint;
}
}
希望对您有所帮助。