- Home /
The question is answered, right answer was accepted
moving a gamobject in vacum
hi guys i have a gameobject that moves up and down and right and left slowly , like vacum movement this is the code ive written , it runs when the movingtime parameter is true but it doesnt work correctly because of moving time boolean is always true and it hasnt got smooth movement
function MoveInVacum()
{
MovingTime = false ;
NextMoveForX = Random.Range(StartPointXLeft , StartPointXRight);
NextMoveForY = Random.Range(StartPointYDown , StartPointYUp);
NextPoint.x = transform.position.x ;
NextPoint.y = NextMoveForY ;
NextPoint.z = transform.position.z ;
this.transform.position = Vector3.Lerp(this.transform.position, NextPoint, .02 * Time.deltaTime);
//print(NextMoveForX);
//yield WaitForSeconds (1);
MovingTime = true ;
}
so , would anyboy please correct my plan for solving this problem , thanx
Use two functions ins$$anonymous$$d of one. Use one function that will only switch the boolean from true to false, vice versa. I'd use the actual movement code inside of Update() function.
$$anonymous$$ovingTime = !$$anonymous$$ovingTime
Will toggle the boolean.
You don't have your if() statements in any function. Place them inside of Update().
And as I stated in my answer, place your true and false statements in a different function.
Answer by Eric5h5 · Aug 30, 2013 at 05:20 PM
I'm not quite sure what you're actually trying to do, but I'm sure it would be far easier with coroutines in any case:
var startPointXLeft = -5.0;
var startPointXRight = 5.0;
var startPointYDown = -5.0;
var startPointYUp = 5.0;
var moveSpeed = 5.0;
function Start () {
while (true) {
var nextMoveForX = Random.Range(startPointXLeft, startPointXRight);
var nextMoveForY = Random.Range(startPointYDown, startPointYUp);
var originalPoint = transform.position;
yield Move(Vector3(transform.position.x, nextMoveForY, transform.position.z));
yield Move(originalPoint);
yield Move(Vector3(nextMoveForX, transform.position.y, transform.position.z));
}
}
function Move (nextPoint : Vector3) {
var startPoint = transform.position;
var rate = 1.0 / Vector3.Distance(nextPoint, startPoint) * moveSpeed;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
transform.position = Vector3.Lerp(startPoint, nextPoint, t);
yield;
}
}
give me a short time to check it , ill feedback you then, right now just thanx and more
Answer by roojerry · Aug 30, 2013 at 05:08 PM
Are you looking for it to slowly move back and forth and up and down? Mathf.PingPong() might be better for you to use than just getting random values