当前位置:网站首页>Unity shot tracking object

Unity shot tracking object

2022-07-05 04:55:00 yoyoHm

The camera moves smoothly with the character

  

// Design content :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {

    public Transform m_TargetTransform; //  The target to be tracked by the camera 
    private float depth = -18f;          //  The front and back position of the lens relative to the character , A negative number means being behind the character ;
    private float height = 40f;          //  The height of the lens relative to the upper part of the character ;

    [SerializeField]
    private float m_Speed = 12f; //  Control the speed of lens tracking , Used to adjust the lens forehead to move smoothly , If the speed is too high , In extreme cases, the target position is directly assigned to the lens , So for the blinking effect of characters like flash , Will bring adverse visual images 


    void Update()
    {

        if (m_TargetTransform != null)
        {
            var targetposition = m_TargetTransform.position + new Vector3(0, height, depth); 
            transform.position = Vector3.MoveTowards(transform.position, targetposition, m_Speed * Time.deltaTime);
        }

    }

    public void SetTarget(Transform target)
    {

        m_TargetTransform = target;
    }
}

原网站

版权声明
本文为[yoyoHm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140626088094.html