![](/img/trans.png)
[英]Unity3D How to make character face secondary camera's direction when secondary camera is activated
[英]How to make character face direction of travel?
有誰知道我可以如何調整我的代碼,使角色面向它行進的方向? 我意識到這個問題可能與現有問題重復,但我不確定在哪里/如何在我的代碼中應用正確的輪換代碼:
coroutineAllowed = false;
Vector2 p0 = routelist[routeIndex].routes[routeNumber].GetChild(0).position;
Vector2 p1 = routelist[routeIndex].routes[routeNumber].GetChild(1).position;
Vector2 p2 = routelist[routeIndex].routes[routeNumber].GetChild(2).position;
Vector2 p3 = routelist[routeIndex].routes[routeNumber].GetChild(3).position;
while (tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
transform.position = characterPosition;
yield return new WaitForEndOfFrame();
}
forward
方向正常。 這就是為什么將它們的局部前向設置為與正交相機的方向正交往往會使它們不可見。從您在其他答案中共享的圖像來看,您似乎需要將角色的局部“向上”方向旋轉到他們移動的相反方向,同時保持角色的局部“向前”方向指向全局向前方向。
您可以使用Quaternion.LookRotation
來執行此操作,盡管您需要對參數有一點創意,將您想要遠離的方向作為up
參數,將全局forward
作為向前參數。
coroutineAllowed = false;
Vector2 p0 = routelist[routeIndex].routes[routeNumber].GetChild(0).position;
Vector2 p1 = routelist[routeIndex].routes[routeNumber].GetChild(1).position;
Vector2 p2 = routelist[routeIndex].routes[routeNumber].GetChild(2).position;
Vector2 p3 = routelist[routeIndex].routes[routeNumber].GetChild(3).position;
while (tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
Vector3 fromPosition = transform.position;
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
transform.position = characterPosition;
Vector3 newLocalUpDirection = fromPosition - transform.position;
transform.rotation = Quaternion.LookRotation(Vector3.forward, newLocalUpDirection);
yield return new WaitForEndOfFrame();
}
您可以像這樣使用transform.LookAt(Vector3)
:
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
transform.LookAt(characterPosition);
transform.position = characterPosition;
嘗試這樣做,首先獲取Lookvector
,然后引導角色查看它。
while (tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
Vector3 v3 = characterPosition;
Vector3 Lookvector = v3 - transform.position;
Quaternion rotation = Quaternion.LookRotation(Lookvector);
transform.rotation = rotation;
transform.position = characterPosition;
yield return new WaitForEndOfFrame();
}
你有沒有看過這個:
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
這也可能在 x 和 z 空間中旋轉,但您可以解決此問題並執行以下操作:
// Direction vector
Vector3 relativePos = target.position - transform.position;
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
// fix rota so that only y rotation occures.
var vectorRota = rotation.EulerAngles;
Vector3 fixedYRotation = new Vector(0f, vectorRota.y, 0f);
// create Quaternion back from Vector3
Quaternion finalRotation = Quaternion.Euler(fixedYRotation );
// assing rotation to GameObject
gameObject.transform.rotation = finalRotation;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.