我在我的播放器和相機之間創建了一個光線廣播,當它檢測到一個命中時,將相機移動到對象的前面,以便該對象不會阻擋視圖。C#Unity Raycast問題,相機沒有響應
調試測試顯示raycast肯定正在工作。然而,我從控制檯得到的關於我的代碼的唯一消息是變量targetMoveUse被賦值,但其值永遠不會被使用。
我從youtube上的教程中學習了這段代碼,並且將它跟在一個tee上,但儘管看起來和教程有相同的代碼,但它並不適合我。本教程僅供參考:https://www.youtube.com/watch?v=0nI0rX5fEY0
這是我的代碼。任何幫助表示讚賞。
Transform player;
Quaternion targetLook;
Vector3 targetMove;
public float rayHitMoveInFront = 0.1f;
Vector3 targetMoveUse;
public float smoothLook = 0.5f;
public float smoothMove = 0.5f;
Vector3 smoothMoveV;
public float distFromPlayer = 5;
public float heightFromPlayer = 3;
void Start() {
player = GameObject.FindWithTag ("Player").transform;
}
void Update() {
targetMove = player.position + (player.rotation * new Vector3 (0, heightFromPlayer, -distFromPlayer));
RaycastHit hit;
if (Physics.Raycast (player.position, targetMove - player.position, out hit, Vector3.Distance (player.position, targetMove)))
targetMoveUse = Vector3.Lerp (hit.point, player.position, rayHitMoveInFront);
else
targetMoveUse = targetMove;
//chapter 4 episode 3 part 1
//transform.position = player.position + (player.rotation * new Vector3 (0, heightFromPlayer, -distFromPlayer));
//transform.position = Vector3.Lerp (transform.position, targetMove, smoothMove * Time.deltaTime);
transform.position = Vector3.SmoothDamp (transform.position, targetMove, ref smoothMoveV, smoothMove);
targetLook = Quaternion.LookRotation (player.position - transform.position);
transform.rotation = Quaternion.Lerp (transform.rotation, targetLook, smoothLook * Time.deltaTime);
//chapter 4 episode 1
//transform.LookAt (player);
}
}
那麼,什麼是不工作更換targetMove變量?你沒有提到那個部分... – Programmer