2019年5月7日 星期二

Week 12

Week 12
接續測試程式碼
二、電腦相機跟隨人物移動

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

public class TheThirdPersonCamera : MonoBehaviour
{
    public float distanceAway = 1.7f;
    public float distanceUp = 1.3f;
    public float smooth = 2f;                      // 跟隨流暢度

    private Vector3 m_TargetPosition;     

    Transform follow;        //the position of Player

    void Start()
    {
        follow = GameObject.FindWithTag("Player").transform;//找到相機的tag
    }
    void LateUpdate()
    {
        //計算攝像機距離人物的距離
        m_TargetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway;

        // 把相機相對坐標還原成世界坐標
        transform.position = Vector3.Lerp(transform.position, m_TargetPosition, Time.deltaTime * smooth);

        //確保相機一直看到人物
        transform.LookAt(follow);
    }
}

沒有留言:

張貼留言