Reputation:
So my problem is that I want my objects to randomly move within a certain round range, with a certain speed. Right now it moves very fast and multiplying it by speed doesn't work.
Here's my code:
using UnityEngine;
using System.Collections;
public class RandomTargetMovement : MonoBehaviour {
public float radius = 20.0f;
void Update(){
transform.position = Random.insideUnitCircle * radius;
}
}
Upvotes: 0
Views: 3454
Reputation: 86
That's because you're making a new random number in every update. This is bad for several reasons.
But in this particular instance, not only it is bad, it simply doesn't work. That's because update is called every time a frame is rendered and that means you will always have jerky motion, no matter how you set your speed. For that, you should use deltaTime.
I assume what you want is for the object to move to a point, then start moving towards a new random point. Here is a not-so-elegant solution:
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;
}
}
Hope this helps.
Upvotes: 1