亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Unity 中以第三人稱視角讓攝像機圍繞玩家旋轉?

如何在 Unity 中以第三人稱視角讓攝像機圍繞玩家旋轉?

C#
qq_遁去的一_1 2022-11-21 16:46:40
我正在嘗試讓攝像機圍繞玩家旋轉,以便玩家始終位于屏幕中間。我試過使用這個Slerp()功能。using UnityEngine;using System.Collections;public class rotate : MonoBehaviour{    public Transform target;    public float Speed = 1f;    public Camera cam;    public Vector3 offset;    void Update()    {        Vector3 direction = (target.position - cam.transform.position).normalized;        Quaternion lookrotation = target.rotation;        Quaternion playerrotation = target.rotation;        playerrotation.y = target.rotation.y;        playerrotation.x = 0f;        playerrotation.z = 0f;        lookrotation.x = transform.rotation.x;        lookrotation.z = transform.rotation.z;        //lookrotation.y = transform.rotation.y;        offset.x = -target.rotation.x * Mathf.PI;        transform.rotation = Quaternion.Slerp(transform.rotation, playerrotation, Time.deltaTime * Speed);        transform.position = Vector3.Slerp(transform.position, target.position + offset, Time.deltaTime * 10000);    }}它有效,但播放器不在屏幕中間。
查看完整描述

1 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

以下腳本將旋轉它附加到的游戲對象,以便將游戲對象保持在Target屏幕的中心,并使相機看起來與目標的方向相同。


public Transform Target;

public float Speed = 1f;

public Vector3 Offset;

void LateUpdate()

{

    // Compute the position the object will reach

    Vector3 desiredPosition = Target.rotation * (Target.position + Offset);


    // Compute the direction the object will look at

    Vector3 desiredDirection = Vector3.Project( Target.forward, (Target.position - desiredPosition).normalized );


    // Rotate the object

    transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( desiredDirection ), Time.deltaTime * Speed );


    // Place the object to "compensate" the rotation

    transform.position = Target.position - transform.forward * Offset.magnitude;

}

注意:永遠,永遠,永遠不要操縱四元數的組件,除非你真的知道你在做什么。四元數不存儲您在檢查器中看到的值。它們是用于表示旋轉的復雜實體,具有 4 個值。


查看完整回答
反對 回復 2022-11-21
  • 1 回答
  • 0 關注
  • 218 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號