The 3dUI or model in unity always faces the camera and rotates with the camera angle of view丨The angle of view follows丨Fixed angle of view

camera following

This script is for a target to follow
the 3dUI or model in unity to always face the camera and rotate with the camera angle of view.
The method of use is super simple, and it is very friendly to an experienced Ctrl+cv program. I don’t need to
explain much and go directly to the code

code module

public class Focus : MonoBehaviour
{
    
    
    // The target we are following
    [SerializeField] private Transform target;

    [SerializeField] private bool once;

    [SerializeField] private bool reverse;

    // The distance in the x-z plane to the target
    [SerializeField] private float distance = 10.0f;

    private void OnEnable()
    {
    
    
        Follow();
    }

    void LateUpdate()
    {
    
    
        if (once) return;

        Follow();
    }

    private void Follow()
    {
    
    
        var forward = target.TransformDirection(Vector3.forward);

        transform.position = target.position + forward * distance;

        var toward = Quaternion.identity;

        toward.SetLookRotation(!reverse ? target.forward : -target.forward, target.up);

        transform.rotation = toward;
    }
}

Use explanation

insert image description here
Our Target mounts the camera
Once this bool: when it is true, when the mounted object is activated, it executes once. The effect is to appear a fixed position and fix the
suspension
. The distance
is simple and practical

Guess you like

Origin blog.csdn.net/weixin_42746271/article/details/123870332