LookAt function in Unity 5

Advertisemen

First create two objects : cube and sphere [ GameObject > 3D Object > Cube/Sphere ] then create one C# script and attached this script to cube.

Now, double click on script to edit then make sure to look like this or just copy and paste the code.

using UnityEngine;
using System.Collections;

public class lookAt : MonoBehaviour {
    public Transform toLookAtObj;
    
    // Update is called once per frame
    void Update () {
        transform.LookAt(toLookAtObj);
        Debug.DrawRay(transform.position, transform.forward*5, Color.green);
    }
}

Now go back to unity and drag and drop sphere into the toLookAtObj slot.


Ok, now when hit play, you should see the cube with green light coming out pointing to sphere. Try moving sphere in Scene view to see cube following the sphere.


Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D