- Home /
 
 
               Question by 
               LordElysian · Dec 18, 2012 at 02:39 PM · 
                transformobjectclicktranslate  
              
 
              Moving an object towards a clicked object
I've been attempting to code a situation where the player is represented by StarShip in the code and when you click on an in-game object, the StarShip moves to the object's position that was clicked on and stops. The code I have right now though has this error where StarShip just slides and never actually stops at the object's position that was clicked on. Any help would be great.
 //StarShip is the object that is moving when the user left-clicks.
 var StarShip: Transform;
 var PlanetLocX; //The X coordinate of the object StarShip is moving to.
 var PlanetLocY;
 var PlanetLocZ;
 var isClicked: boolean = false;
 
 function OnMouseDown () {
     PlanetLocX = transform.position.x;
     PlanetLocY = transform.position.y;
     PlanetLocZ = transform.position.z;
     isClicked = true;
 }
 
 function Update() {
     if(isClicked == true){
         //Move StarShip to the clicked object's position.
         StarShip.Translate(PlanetLocX* Time.deltaTime, PlanetLocY* Time.deltaTime, PlanetLocZ* Time.deltaTime);
         }
     }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by PAEvenson · Dec 18, 2012 at 03:41 PM
Try lerping to the position while they are clicking: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
Your answer