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.

Unity3D tutorial | Game Object Looking at another gameobject

This tutorial will help you find how your one game object can look at another object at all times.

Scenario: 

We have enemies and player in a scene. Player is constantly moving from one place to another. Enemies should be intelligent enough to look towards the player every frame.

Solution:

This can done like:


function Update () {

 // get direction of player
        _direction = (Target.gameObject.transform.position - transform.position).normalize
         
        // create the rotation we need to be in to look at the target
        _lookRotation = Quaternion.LookRotation(_direction);
         //rotate us over time according to speed until we are in the required rotation
         transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);

}


Throw this script of any enemy object and pass the "Target" variable of player as reference to it. And it is all done

If any question, plz ask me.

Monday 11 May 2015

Welcome to Programmers Punch

Programming guide is the reason i created this blog. I will be sharing C#, PHP, JS and Unity3D coding practices and solutions to programmers out there.

I am a professional Unity3D, C# and JS developer having experience in web field as well.

Stay tuned for coming codes and do share with friends.