ham
August 29, 2013, 6:57pm
1
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
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;
}
}
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