[問題] 如何在unity裡實現攝影機跟在車子後方?

作者: rede1420 (星晴)   2017-11-27 02:51:07
如同題目所說的
想要在unity裡面實現攝影機跟在車子後方且轉彎後還是只照車子後方
目前撰寫的程式如下
using UnityEngine;
using System.Collections;
public class CameraMotor : MonoBehaviour
{
public Transform target;
public float distance = 10.0f;
public float height = 7.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
if (target)
{
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle,
wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight,
heightDamping * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler( 0,
currentRotationAngle, 0);
Vector3 pos = target.position;
pos -= currentRotation * Vector3.forward * distance;
pos.y = currentHeight;
transform.position = pos;
// Always look at the target
transform.LookAt(target);
}
}
}
以上是按照網路上的教學寫的,執行之後發現攝影機一直維持是在車子的右方
嘗試修改了程式碼,還是會發生錯誤
想問問要怎麼修改才會使得攝影機會在跟在車子的後方並且隨著車子一起轉向?
謝謝各位大大

Links booklink

Contact Us: admin [ a t ] ucptt.com