- Home /
transforming position
I'm trying to transform the position of an object once a variable in another script becomes true, but of course, it doesn't work properly, it starts from position B once the variable (csScript.wave) becomes true. Here is my code;
var pointB : Vector3;
var pointC : Vector3;
var pointD : Vector3;
var pointE : Vector3;
var pointF : Vector3;
var csScript : TargetCollision;
// private var endPos : Vector3 = new Vector3(0.0, 40.0, 0.0);
function Start () {
//animation.Play("running");
var pointA = transform.position;
while (true) {
//while(csScript.wave){
//if(csScript.wave){
yield MoveObject(transform, pointA, pointB, 3.0);
yield MoveObject(transform, pointB, pointC, 3.0);
yield MoveObject(transform, pointC, pointD, 3.0);
yield MoveObject(transform, pointD, pointE, 3.0);
yield MoveObject(transform, pointE, pointF, 3.0);
// }
}
}
function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3 , time : float) {
if(csScript.wave){
transform.position = startPos;
transform.LookAt(endPos);
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate/8;
// if(csScript.wave)
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
}
Answer by Gruffy · Jan 07, 2014 at 09:04 PM
pull your "while(true)" conditional code out of start() and whack into Update().. it is only running the code once and consequently only moving it a single time (3 units per frame/second). After that the code is never called again. Why? Because Start() is an initialisation method. update is the game loop, this is where code will be read over and over again.
BTW, what is a MoveObject, something I have never heard of outside of bespoke code for Unity. Normally, you call to transform.Translate(); feeding the argument with vector positions and float distance...like so...
transform.translate(this.transform.position, objectToMoveTo.transform.position, 3.0f );
Your objectToMoveTo would be another GameObject/Transform that you have specified (perhap as a public GameObject in your script that you could assign in the inspector to the script etc. just comment and I will get back to you if not, else if it answered your question help others to find it too by marking it an answer.
there is another way, a better way... LERP
This allows something known as linear interpolation between to specified objects and a float value for the length of time to take getting from one point to another. Check out the reference above, it is the way forward for you, with respect to better handling of your transforms translations (at this point anyway)
take care bud.
Gruffy.
Thank you for your answer, I just created that function to sort of make way points, it is moved from point A - B then B - C so on, with the help of that $$anonymous$$oveObject function in the bottom of the script there. I feel like your code will work, thanks again, will grant your answer once I change function name Start to Update.
You would need to call the function "$$anonymous$$oveObject" from Update, this will continue to call the loop you have created in your code to translate the object. You would also be then using the positions you have made in Start for your way points as an iterative value that gets evaluated against its current position. something like....
for(int i = 0; i< waypoints.length i++) { Transform nextWp = new Transform(); nextWp = waypoints[i+1]; transform.LookAt(nextWp ); transform.Translate(waypoints[i].position);
}
The above isn`t a resolve for your issue, just a guide as such.
here is a script package that you should really take a peek at. It is written in Java/UnityScript but i can also pop over a C# version that I had to implement a while back. I see you are using UnityScript though so thought this was best
import it and load the scene, press play and have a look at whats going on there.
Take care, hope this solves it for you :) Gruffy waypoints
Your answer
Follow this Question
Related Questions
Change position of camera on scene load? 1 Answer
Create a straight gradient equation with a grid of 3d objects 1 Answer
Following another object's position/rotation like parent/child relationship? 4 Answers
How do you set the initial value of a variable to an objects current position in unity? 1 Answer
Create an animation with variables? 1 Answer