- Home /
Question by
flowythekid · Jun 02, 2013 at 03:52 PM ·
beginnerloopfor-loopwhile
Beginner: Box moving up and down. Loop?
Hey guys, I have a basic box moving up and down and I can't figure out how I will get it to work. Am trying on that for days now. It rotates and it moves up but I just want it to move up x-units and then move down in an endless loop. A gravitating platform like in old jump and runs etc.
Thanks in advance!
The code I have so far is here:
var rotateSpeed : float = 10;
var moveUpSpeed : float = 5;
function Update()
{
transform.Rotate(0, rotateSpeed * Time.deltaTime, 0);
transform.Translate(0, moveUpSpeed * Time.deltaTime * 0.2, 0);
}
Comment
Answer by FL · Jun 02, 2013 at 04:29 PM
var rotateSpeed : float = 10;
var moveUpSpeed : float = 5;
var distance : float = 0.0;
var maxDistance : float = 100.0;
var direction : Vector3 = Transform.up;
transform.Translate(0, moveUpSpeed * Time.deltaTime * 0.2, 0);
function Update()
{
if(distance>=maxDistance){
distance=0;
direction*= -1;
}
var movement : float = moveUpSpeed * Time.deltaTime * 0.2 * direction;
transform.Translate(movement);
distance+=movement;
}
Loop occurs after the object moves 100 of distance
Your answer
![](https://koobas.hobune.stream/wayback/20220613103402im_/https://answers.unity.com/themes/thub/images/avi.jpg)