MechaScoots
MechaScoots

Reputation: 75

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
}

Upvotes: 0

Views: 1261

Answers (2)

Milad Qasemi
Milad Qasemi

Reputation: 3059

 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);
}

Upvotes: 1

giacomelli
giacomelli

Reputation: 7415

To slowly move your object, you can use Vector3.Lerp to interpolate from current position to target position.

Vector3.Lerp(Vector3 from, Vector3 to, float t);

transform.position = Vector3.Lerp(currentPosition, targetPosition, 0.1f);

Upvotes: 0

Related Questions