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);
}
}
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
Tidak ada komentar:
Posting Komentar