- Home /
How do I add a vector3.lerp or movetoward
Not answered fully yet constantly updated.
I have a game object that has 10 specific cord positions (specific coordinates located below declared var. I need it to move randomly between these positions like every 20 seconds and make them move over time using lerp or something else, i dont know how to bring it all together i have most of it main thing is start and end coordinates var location = transform saves the location i believe but is it in right place? and would i put that variable in starting marker for lerp?
need help with at least position1/random pick 1 after that i should be able to fill in rest
any help appreciated im a beginner to coding.
#pragma strict
// Transforms to act as start and end markers for the journey.
var start$$anonymous$$arker: Transform;
var end$$anonymous$$arker: Transform;
var Position1: Transform;
// cord for P1 98.197 on y axis
var Position2: Transform;
// cord for P2 99.188 on y axis
var Position3: Transform;
// cord for P3 100.182 on y axis
var Position4: Transform;
// cord for P4 101.173 on y axis
var Position5: Transform;
// cord for P5 102.1631 on y axis center
var Position6: Transform;
// cord for P6 103.156 on y axis
var Position7: Transform;
// cord for P7 104.147 on y axis
var Position8: Transform;
// cord for P8 105.138 on y axis
var Position9: Transform;
// cord for P9 106.130 on y axis
var Position10: Transform;
// cord for P10 107.118 on y axis
{
//select a random number, inside a maths function absolute command to ensure it is a whole number
var randomPick : int = $$anonymous$$athf.Abs(Random.Range(1,11));
//create a location 'Transform' type variable to store one of 10 possible locations declared at top of script
var location : Transform;
//check what randomPick is, and select one of the 10 locations, based on that number
if(randomPick == 1){
location = Position1;
Debug.Log("Chose pos 1");
}
// $$anonymous$$ovement speed in units/sec.
var speed = 1.0;
// Time when the movement started.
private var startTime: float;
// Total distance between the markers.
private var journeyLength: float;
var target : Transform;
var smooth = 5.0;
function Start() {
// $$anonymous$$eep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(start$$anonymous$$arker.position, end$$anonymous$$arker.position);
}
// Follows the target position like with a spring
function Update () {
// Distance moved = time * speed.
var distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
var fracJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
transform.position = Vector3.Lerp(start$$anonymous$$arker : transform , end$$anonymous$$arker : Position1, fracJourney);
}
else if(randomPick == 2){
location = Position2;
Debug.Log("Chose pos 2");
}
else if(randomPick == 3){
location = Position3;
Debug.Log("Chose pos 3");
}
if(randomPick == 4){
location = Position4;
Debug.Log("Chose pos 4");
}
if(randomPick == 5){
location = Position5;
Debug.Log("Chose pos 5");
}
if(randomPick == 6){
location = Position6;
Debug.Log("Chose pos 6");
}
if(randomPick == 7){
location = Position7;
Debug.Log("Chose pos 7");
}
if(randomPick == 8){
location = Position8;
Debug.Log("Chose pos 8");
}
if(randomPick == 9){
location = Position9;
Debug.Log("Chose pos 9");
}
if(randomPick == 10){
location = Position10;
Debug.Log("Chose pos 10");
}}
Answer by theredace · May 02, 2014 at 03:22 PM
You could make an array of Vector3's that contain all your positions. Then every time you want to randomly move to a new position, use Random.Range to get a random number between 0 and 19 and use that randomized number as the array index for your target position that you lerp to. I'm a C# guy, but something like this:
Vector3[] positions;
int rand = Random.Range(0, 20); //the "20" is not included in the possible random values
transform.position = Vector3.Lerp(transform.position, positions[rand], Time.deltaTime);
kinda not following you, i only have 10 actual positions and its all on the y axis, i assume i need to add the transform.position to all the if statements IE
if(randomPick == 1){
location = Position1;
Debug.Log("Chose pos 1");
}
using position one as example the coordinate for it is 98.197 on y axis how would i make it move over time from its current point to position 1, also assu$$anonymous$$g i have the code right to save the positions its in, i figure if i get one right its same for all positions
Sorry, I got the numbers mixed up. I was thinking it was 20 positions ins$$anonymous$$d of 10. There are a variety of ways to move something over time, just depends on the type of movement you want. Vector3.$$anonymous$$oveTowards is straight linear movement, Vector3.Lerp slows down at the end, Vector3.SmoothDamp speeds up at the beginning and slows down at the end, etc. However you do it, you just need to make sure you're putting it in code that's repeating every frame so it continues to move. Like use a coroutine:
if(randomPick == 1){
location = Position1;
StartCoroutine($$anonymous$$ove(location));
}
IEnumerator $$anonymous$$ove(Transform loc)
{
bool moving = true;
while (moving)
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, loc.position, Time.deltaTime)
yield return null;
if (transform.position == loc.position)
moving = false;
}
}
Something like that. Again though, this is C#. You could change to a Lerp ins$$anonymous$$d of a $$anonymous$$oveTowards, but you'd have to use a different way to check if the position is "near" the end position, because with a lerp it'll never actually read loc.position.
<<<< so lost <<<< well lerp seems easy idk how slow it will be when it gets to end but there is a start point and end point of the movement, i also wanna make sure that before the function runs its storing the location so it knows where its starting the animation. Do i need to put a var telling what each coordinate is for each position? that away it knows its at this position and it knows im starting at this coordinate and need to go to this one.
You should go read up on coroutines. You could technically do the Lerp in your if statement since it's within the Update() method and will be called every frame, but it's an inefficient way of doing it. You ideally want to separate that Lerp code out into a coroutine and only call it once from your Update() method.
As for the starting position, yeah just create a new Vector3 variable and store the current transform.position into it right before you start the movement so that you have a variable for your starting position even after you move.
//create a location 'Transform' type variable to store one of 10 possible locations declared at top of script
var location : Transform;
does this do just that i feel that i need to add the coordinates in the variable but how would i do that?
and as for moving would
transform.position = Vector3(0, 98.197, 0); work? I would just need to figure out how to make it move overtime and store position
sorry please do into detail on how to do these things im very new to coding lol i get how it should work concept wise just not how to write it out and where to put it.
EDIT i was just looking at stuff and found this
Transform.position = Vector3.Lerp(startPos : transform , endPos : position1);
providing it saves current location how would i tell it to update current location or is that part of the code i already have before it runs the function?