About

Monday 18 May 2015

Unity3D Tutorial | Game object move towards another game object

One game object move towards another game object in Unity3D tutorial:

I am gonna show you how you can move one game object towards another in Unity3D script via java script.

Scanerio:

Suppose there are enemies in the scene and you want them to move towards a certain position when game starts.

Game object move towards another game object
                         Game object move towards another game object


Solution:

This is how you can do it:

take a reference variable target and give target position to it in hierarchy.


function Update () {

step = speed * Time.deltaTime;

var diff = target - transform.position;

// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target, step);

}



Attach this script of game object and it will move towards in the speed towards the target.


Ask any question to me in comments.

No comments:

Post a Comment