Search code examples
unity-game-engineunityscriptunity3d-2dtools

Move an object in unity2D towards another clicked object


  • I have an object called 'Player' in my scene.
  • I also have multiple objects called 'Trees'.

Now I'd like whenever the user clicks on a 'Tree', the 'Player' to move slowly to that position (using Lerp or moveTowards) is both O.K for me.


Now I have 2 issues with this code:

I'd like this code to be generic

Whenever I click a tree object I'd like this to move the player towards that tree. I don't want to write up this script and attach it to each tree object.

  • Where should I put the script?

    Currently I attach this code to every tree object.

  • How should I write it down so that it applies to every tree object in the scene?

If another click is made while moving, cancel previous movement and start moving to new position

  • How should I write it so that if I click on another object while the player is moving towards another object that was clicked, the players stops moving towards it's previous position, and starts moving towards the new point.

I'm having some trouble adjusting to the new UnityScript thingy. I come strictly from a Javascript background and it really seems like the 2 of them are languages with very different semantics. So if someone answers this with code(which is what I would want:) ), I'd appreciate some verbose comments as well.


I currently do this:

var playerIsMoving = false;
Public playerObject: Gameobject; //I drag in the editor the player in this public var

function update(){  

   var thisTreePosition = transform.point; //this store the X pos of the tree
   var playerPosition = player.transform.point;

   if(playerIsMoving){
    player.transform.position = Vector2.MoveTowards(playerPosition, thisTreePosition, step);
   }

}

function OnMouseDown(){
    playerIsMoving = true;
}

I'm writing this from home where I don't have Unity and I forgot the code syntax therefore expect the above code to have typos or issues, at work it works just fine, apart from being very crude and unsophisticated.


Solution

  • Okay, so I've done it myself (with some help from Freshchris's answer).

    Here it is:

    var target:Vector2;
    var targetObject:GameObject;
    var initialY:float;
    var tolerance = 1;
    var speed = 3;
    var isMoving = false;
    
    function Start(){
        target = transform.position; 
    }
    
    function Update () {
    
        if (Input.GetMouseButtonDown(0)) {
            var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //raycast the scene
    
            // if we hit something
            if (hit.collider != null) {
                isMoving=true; //it should start moving
                targetObject = hit.collider.gameObject; //the target object 
                target = hit.collider.gameObject.transform.position;
            }
        }
    
        // The step size is equal to speed times frame time.
        var step = speed * Time.deltaTime;
    
        // if it should be moving - move it - and face the player to the correct direction
        if (isMoving){
            if(transform.position.x>target.x){
                gameObject.transform.localScale.x =  -0.7;
            }else {
                gameObject.transform.localScale.x =  0.7;
            }
            transform.position = Vector2.MoveTowards(transform.position, target, step);
        }
    
        // if it reached the target object by a specified tolerance, tell it to stop moving
        if(isMoving){
            if((transform.position.x < target.x+tolerance)&&(transform.position.x > target.x-tolerance)){
                print("position reached"); //this is fired just once since isMoving is switched to false just one line below
                print(targetObject);
                isMoving=false;
            }
        }
    }