3
我需要從我的玩家角色,到我的相機raycast的命中對象進行raycast,但我不能讓我的代碼工作,它似乎總是偏移。Raycasting第三人稱射擊
void Update() {
RaycastHit hit;
Ray playerAim = playerCamera.GetComponent <Camera>().ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2));
Physics.Raycast(playerAim, out hit);
Debug.DrawRay(playerAim.origin, playerAim.direction * hit.distance, Color.green);
if (Physics.Raycast(playerAim, out hit)){
// something was hit
RaycastHit playerHit;
Physics.Raycast(transform.position, hit.transform.position, out playerHit);
Debug.DrawRay(transform.position, playerHit.transform.position, Color.red);
Debug.Log ("Distance from camera: "+ hit.distance);
Debug.Log ("Distance from player: "+ playerHit.distance);
}
}
http://i.stack.imgur.com/HzdVU.jpg
我想Debug.DrawRay(transform.position, playerHit.transform.position, Color.red);
應該吸取我的球員到綠線攝像機命中點的最後一根紅線,而是過去拍攝到地面。