[问题] 如何在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