how I can create an object that always look at the camera?
Like this tree:
Thanks in advance!
how I can create an object that always look at the camera?
Like this tree:
Thanks in advance!
I spent some time researching the issue of billboard sprites. I personally believe that the correct course of action is not to just perform a Transform.LookAt, but to actually align all billboard sprites to point the same direction that the camera is pointing. If you simply have each sprite look at the camera, multiple billboards across your screen will create a curve around your camera and they jut into each other if they overlap; This is because each billboard sprite would exist in 3D space at slightly different angles. Worse still, objects on the edge of the screen get warped like a funhouse mirror. Therefor aligning seems to be the way to go.
MyTransform.forward = MyCameraTransform.forward;
I also agree with marsbear that there may be a benefit to putting the code in LateUpdate.
Pictures and reference code available here:
using UnityEngine;
public class Billboard : MonoBehaviour
{
void Update()
{
transform.LookAt(Camera.main.transform.position, -Vector3.up);
}
}
Call it Billboard.cs and drop it on any object you want to be a billboard and/or make a prefab.
Just use the quaternion rotation of the camera for orienting sprite.
As simple as:
sprite.transform.rotation = Camera.main.transform.rotation;
The technique you are referring to is known as 'billboarding' On the Unity terrain editor you can create trees and set their billboarding settings.
If you want your billboard to stay upright so it doesn’t tilt back to look up at the camera when it approaches, you need to modify DaveA’s solution so your target position has no height changes:
public class Billboard : MonoBehaviour
{
void LateUpdate()
{
var target = Camera.main.transform.position;
target.y = transform.position.y;
transform.LookAt(target);
}
}
All this scripts are extremely expensive for memory… But we have a good trick to solve this:
Check out my Games&Assets: https://oris-romero.itch.io/
To improve performance, turn off the Update() while the object isn’t visible:
public class Billboard : MonoBehaviour
{
void OnEnable() {
if(!GetComponent<Renderer>().isVisible) {
enabled = false;
}
}
void LateUpdate()
{
transform.forward = Camera.main.transform.forward;
}
void OnBecameVisible() {
enabled = true;
}
void OnBecameInvisible() {
enabled = false;
}
}