- Home /
Make Object Move to another Object
I am trying to move one object to another object in my game. I have a function that does this when I hold down the "1" button on my keyboard. However I have to go into unity and pick the start object and the end object. Is there a way to define the start and end object in the script?
var start : Transform;
var end : Transform;
function Update () {
if (Input.GetKey("1")){
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
}
Answer by koray1396 · Apr 01, 2014 at 05:19 PM
there are many ways.
Find Finds a game object by name and returns it.
FindGameObjectsWithTag Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTag Returns one active GameObject tagged tag. Returns null if no GameObject was found.
there is an example below. http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindWithTag.html
also below you will find methods for detecting other objects. http://docs.unity3d.com/Documentation/ScriptReference/Physics.html