- Home /
Move object, then destroy it.
Hello Unity Masters,
I have a quick question that I am trying to get an answer to... I'm trying to get a gameObject (named "Hall") to move from Point A to Point B, then destroy itself (gets removed from the scene when it gets to Point B.)
If anyone could help me out with this problem, I would greatly appreciate it! THANK YOU!
Comment
Answer by CodeMasterMike · Dec 19, 2012 at 06:30 AM
A very simple example:
private Vector3 translateVector = new Vector3(1.0f, 0.0f, 0.0f);
private Vector3 startPosition = new Vector3(10.0f, 0.0f, 0.0f);
private Vector3 goalPosition = new Vector3(20.0f, 0.0f, 0.0f);
private bool useLerp = true;
public GameObject playerCharacter = null;
Start()
{
playerCharacter.Transform.position = startPosition;
}
update()
{
if(playerCharacter != null)
{
if(useLerp == true)
{
playerCharacter.Transform.position = Vector3.Lerp(startPosition, goalPosition, Time.time);
}
else
{
playerCharacter.Transform.Translate(translateVector);
}
if(playerCharacter.Transform.position == goalPosition)
{
Destroy(playerCharacter);
}
}
}
Basically, this example you can use either Lerp or Translate. Both works fine but works in different ways.
Good luck!