Search code examples
unity-game-engineunityscript

Unityscript Make object move slowly


So I have no idea why this isn't working. I've asked my teacher but he's not at all helpful. In my game, I have an object, that when triggered, causes a wall to retract vertically. However, no matter how I fiddle with it, the object just automatically snaps to another location with no relation to where it just was. (I am able to get the object back just fine with different script to reset things). I added two functions to it so instead of instantly moving to the new location, it would slowly move, but it doesn't seem to be working at all.

var door: GameObject;
var torch: GameObject;
var flame: GameObject;

function OnMouseDown(){
for(var count: int = 0; count < 10; count++)
    {
    door.transform.position = Vector3(0,0.1,0);
    torch.transform.position = Vector3(0,0.1,0);
    flame.transform.position = Vector3(0,0.1,0);
    yield WaitForSeconds(1.0);
    }
// move door out of way
}

Solution

  •  Vector3 dest=new Vector3(0,1,0); //set your destination position here , i set (0,1,0) set whatever
    public float smooth=2.0;
    
    function OnMouseDown(){
    door.transform.position = Vector3.Lerp (door.transform.position, , Time.deltaTime * smooth);
    torch.transform.position = Vector3.Lerp (torch.transform.position, , Time.deltaTime * smooth);
    flame.transform.position = Vector3.Lerp (flame.transform.position, , Time.deltaTime * smooth);
    }