Have object always lookAt camera
I'm a total newbie with C# and I'm trying to modify this script so that instead of having to manually assign a target to the script in the editor, it would just assign the main camera to be the object to look at. I have tagged my camera with the MainCamera tag in case it helps. Here is the code that I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class alwaysFaceCamera : MonoBehaviour {
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}
Answer by UnityQuestion2017 · Oct 05, 2017 at 06:58 PM
Here's what seems to work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class alwaysFaceCamera : MonoBehaviour {
private Transform camera;
// Use this for initialization
void Start () {
camera = Camera.main.transform;
}
// Update is called once per frame
void Update()
{
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(camera);
}
}
What if the main camera is always changing, is there any better solution instead of calling Camera.main in Update()?
Your answer
Follow this Question
Related Questions
How to set far and near clipping plane of camera with scripting 1 Answer
randomly Instantiate items 0 Answers
Rotating a player around the centre with touch controls. 0 Answers
How do you continuously rotate an object? 1 Answer
Timer wont work 0 Answers