hopefully somebody has an answer for this because it driving me effin mad ; )
i’m fooling around with the noise move script, which also calls perlin noise (both from the procedural example zip), and i can’t figure out why it always snaps the object .5 meter to one side before “randomizing”. it even does this in the example scene from the zip. what is doing this? i’ve tried changing everything i can think of, commenting out lines, i even started poking around in perlin.cs, but whatever i do i can’t make this go away.
scripts attached if you don’t have the procedural folder, just throw the NoiseMove on a cube to see the effect (have the perlin in standard assets so it compiles first).
35301–1290–$perlin_176.cs (13.9 KB)
35301–1291–$noisemove_206.js (398 Bytes)
The problem is basically this line:
transform.localPosition = offset + basePosition;
Actually that line is fine, but the concept has a flaw. Unless offset just happens to start out very close to (0, 0, 0), you’re going to get a snap to another location. After that first move, offset changes little from frame to frame, so it’s fine. So the problem is how to avoid that first jump. One solution is to call the noise function first and store that result, and use the result as a base to get a relative movement instead of an absolute movement. Try this:
var speed = 1.0;
var moveSize = Vector3.one;
private var basePosition : Vector3;
private var baseOffset : Vector3;
function Start () {
basePosition = transform.localPosition;
baseOffset = SmoothRandom.GetVector3(speed);
}
function Update () {
var offset = baseOffset - SmoothRandom.GetVector3(speed);
offset = Vector3.Scale(moveSize, offset);
transform.localPosition = offset + basePosition;
}
@script AddComponentMenu("Noise/Transform Position")
nice eric, that’s it. i really was tearing my hair out over this one, i spent 2 hours and just couldn’t work it out - thank you so much ; )
No problem.
One thing that occurs to me, though, is that using the absolute method means the object is permanently constrained to a certain area. Changing it to relative movement means there’s a chance it could theoretically wander off anywhere, eventually (I think). If that’s going to be a problem, you might want to throw a Mathf.Clamp or three in there somewhere.
–Eric