- Home /
Make Prefab Look At Target
I need an LOD in a prefab to look at a specific game object (the Camera). Currently I have it set up to where it looks at a public variable. However, this variable has to be set for every prefab even if I set it for one prefab and attempt to override the change. I don't want to go through the hundreds of prefabs in my scene a manually assign the target for each prefab. So, how would I script it to make all of the prefabs look at a specific gameObject rather than making them look at a public target?
Answer by Stevphen · Dec 03, 2020 at 08:11 PM
Thanks for the response. I've actually already tried that as well, but I get the error " cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.Transform' "
So try add this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Naovai : $$anonymous$$onoBehaviour
{
Transform cam;
Camera mainCam;
void Start()
{
mainCam = (Camera)FindObjectOfType(typeof(Camera));
cam = mainCam.GetComponent<Transform>();
}
void Update()
{
transform.LookAt(cam);
}
}
Answer by Pokedlg3 · Dec 03, 2020 at 07:34 PM
Try to add this in the scripts of the prefabs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Naovai : MonoBehaviour
{
private GameObject Camera;
void Start()
{
Camera = GameObject.Find("MainCamera");
}
void Update()
{
transform.LookAt(Camera);
}
}
Remember to ADD, do not change the script.
Your answer
Follow this Question
Related Questions
GameObject transform relative camera 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Instantiate cube on network 0 Answers
How to turn camera using LookAt() only along X and Y axis? 1 Answer