[英]Rigidbody.Addforce in"oncollisionenter" is not executing in unity c#
當我試圖制作一個游戲時,當球員與球碰撞時球員踢球並且當我按下 Q 但它不起作用並且沒有顯示任何錯誤
public Rigidbody rg;
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name=="ball")
{
if(Input.GetKeyDown(KeyCode.Q))
{
rg.AddForce(100,0,0);
}
}
}
可能會嘗試向球添加另一個球體對撞機,並使其成為觸發器
那么這是一個改變的問題:
public Rigidbody rg;
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name=="ball")
{
if(Input.GetKeyDown(KeyCode.Q))
{
rg.AddForce(100,0,0);
}
}
}
至:
public Rigidbody rg;
void OnTriggerEnter(Collision col)
{
if(col.gameObject.name=="ball")
{
if(Input.GetKeyDown(KeyCode.Q))
{
rg.AddForce(100,0,0);
}
}
}
這個想法是統一的碰撞是挑剔的,所以你最好的選擇是有一個更大的觸發器來檢測球員是否在球的范圍內
旁注:我猜這是在你的播放器上,所以也許你也可以試試這個:請記住,“if”語句只是為了防止球 object 有不同的擴展名,或者可能是“球(克隆)“ ETC。
Rigidbody rb;
void Start(){
rb = gameObject.GetComponent<Rigidbody>();
}
void OnTriggerEnter(Collision col){
if(col.gameObject.name.ToString().Contains("ball") &&
Input.GetkeyDown(KeyCode.Q)) // this is comp expensive
{
//here you can add force to the player, or to the ball
// for the player :
rb.AddForce(100,0,0);
// for the ball
col.GetComponent<Rigidbody>().AddForce(100,0,0); //REALLY EXPENSIVE
}
}
這應該讓您朝着不同的方向邁出一步,希望這會有所幫助! 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.