- Home /
making an object move up and down
hello, I have an object i want to pop up and down through the terrain/ hole in the terrain, like the spikes that go up and down in the sonic the hedgehog games.
ideally i would like the spikes to pop up wait a few seconds then pop back down for a few seconds.
any ideas how to script this (i tend to code in javascript)?
thanks matt
Answer by dibonaj · Aug 19, 2011 at 06:55 PM
To make an object move up and down you mess with the transform of the gameobject. Since you want to move the object mess with the position. http://unity3d.com/support/documentation/ScriptReference/Transform-position.html
In order to get something to happen after a few seconds use the yield and waitforseconds command. http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html
Answer by DaveA · Aug 19, 2011 at 07:13 PM
You can also use the Animation Editor to create the movement, then a smaller JS to trigger it
Or if it is something you always want playing you won't even have to worry about a JS to trigger it. Just check the "Play Automatically" box and set the wrap mode to Loop.
Answer by vfxjex · Sep 23, 2013 at 09:38 AM
pragma strict
ar upDown:float; var speed:int = 2; var hightDiv:int = 150 var t:float; function Start () {
}
function Update () {
transform.Translate(Vector3.up upDown); t += speed (Time.deltaTime); upDown = (Mathf.Sin(t))/hightDiv;
}