- Home /
move object on x position to new specific x position
My goal here is to move and object from it's starting position on creation, to a targeted x position which is chosen at random from an array which contains a mix of locations.
I have a variable which contains a collection of random points in 3d space.
variable of random locations: var pointsCol = [0,5,10],[10,15,30],[5,3,0]
objects starting location: startPos = [0,0,0]
objects targeted end location: endPos = [10,15,30]
find the difference in the two "X" positions: stepX = (startPos.x - endPos.x)
divide the difference in positions by "10": stepX = (startPos.x - endPos.x) / 10
result: 1
My syntax is not right by any means but i was hoping I could get some help on this. I'm doing it in javascript. I appreciate the help. Thanks
Answer by TehWut · Jul 19, 2012 at 10:16 PM
alright, I'm no coder, but this might work.
vecArray:Vector3[]
this will create an array of vector3 positions to store. set the length in the inspector (three in your case).
Then you could do this in code, or through the inspector.
vecArray[0] = Vector3(5,5,5); // or whatever your positions are
once you have all 3 vectors, you could pick a position like this.
transform.position = vecArray[Random.Range(0,vecArray.length)];
what this does is pick a random vector3 from the array using random.range.
Now you can apply the rest of your functions for calculating positions and other things.hope this helps! If you need further assitance, I'd be happy to help.
EDIT: whoops, looks like I misunderstood your question entirely! I'll work this out and get back to you!
vecArray:Vector3[];
vecArray[0] = transform.position;
vecArray[1] = vecArray[Random.Range(0,vecArray.length)]; // picks a random goal position
var goalPos:Vector3 = ((vecArray[1] - vecArray[0]) / 10) // you may have to reverse the vectors, or use Mathf.Abs for absolute values
transform.position.x = goalPosition.x;
Something like this?