Greetings,
I am having trouble getting smooth random movement, it always ends up very jerky like a hyperactive fly rather than smoothly moving around. I’ve tried this:
function Update() {
transform.Translate(Vector3(UnityEngine.Random.Range(-0.1f,0.1f),0,0)*(Time.time/10));
//tried lots of different values for Time.time, doesn't seem to change much if at all
}
and this
function Update () {
var ran: float = UnityEngine.Random.Range(-1f,1f);
transform.position = Vector3.MoveTowards(transform.position, transform.position+Vector3(ran,ran,ran), 1.0f);
}
and this
var smooth:float = 5.0f;
function Update(){
transform.position = Vector3.Lerp(transform.position, transform.position+Vector3(ran,ran,ran), Time.deltaTime*smooth);
}
All of these approaches work, as in they produce random movements in 3D space, but they are extremely jerky, or flickery. I would like the object to smoothly move around in 3D space.
I would appreciate any insights anyone may be able to offer.
Although I wouldn’t know how to code it, the method I use in 3d animation is similar, but a step beyond what you’ve done.
If you always move from one position to the next, it will always stop-start, or change direction on the exact spot that was randomly generated.
What you’ll want to do is create at least two points in advance, and generate a spline between them, so there will be a smooth transition through the next point.
Just not sure how to create spline math in unity.
*Another trick i used once was to create a random point, measure the distance between that point and where the object is, and use that as a radius, and rotate your object around the point.
Forgot to mention, for that second method to work, the random point needs to be perpendicular to the objects current trajectory. The maths isn’t much fun, but it gives nice motion.
I think I see what you mean, but it seems to me the problem is more with the time taken to move to each point - since it’s in Update(), it’ll calculate a new point and move to it every single frame, thus the jerky movement. I thought this could be solved by playing with Time.time or Time.deltaTime but they just don’t seem to have much effect.
What I’d like is to be able to say “choose a random point and take 1 second to move there, then keep doing that”, but I just can’t seem to work out how to do that.
This thread has one approach that does what I want, I should’ve searched harder the first time:
http://forum.unity3d.com/threads/98734-Moving-Object-around-screen