2016-08-13 61 views
0

我對子彈的碰撞問題敵人我有錯誤的碰撞上子彈射擊

我使用的子彈腳本

子彈腳本的OnCollisionEnter()方法:

public class BulletScript : MonoBehaviour 
{ 

public float TimeForDestory = 10f; 
public float Speed = 0.5f; 

// Use this for initialization 
void Start() { 

} 

public void OnCollisionEnter(Collision col) 
{ 
    Debug.Log("Collision"); 
    if (col.gameObject.tag == "Enemy") 
    { 
     Debug.Log("Collision"); 
    } 
} 

// Update is called once per frame 
void Update() { 
    transform.Translate(0, -Speed, 0); 
    if (TimeForDestory < 0f) 
    { 
     Destroy(gameObject); 
    }else 
    { 
     TimeForDestory -= 0.1f; 
    } 

} 
} 

子彈不碰撞任何東西

子彈不在對象中,因爲我在播放器腳本中使用Method Instantiate()

播放器腳本:

public class Player : MonoBehaviour 
     { 

//Player 


//Move 
public float defualtSpdPlayer = 1f; 
public float plsSpd = 0.1f; 
public float maxPlayer = 2f; 
private float speed = 0; 

//Bullet 
public Transform bulletSpawn; 
public GameObject bullet; 
public Texture ImgBackground; 
public Texture ImgWhiteGround; 
public Texture ImgReloading; 
public float bulletDamge = 1f; 
public float bulletSpeed = 0.1f; 
public float ReloadTime = 0.5f; 
public float ReloadFillAmontX = 2f; 
private float reload = 0; 
private float reloadFillAmont = 0; 
private bool readyToShoot = true; 
private string status = "Ready"; 

//GUI 
[HideInInspector] 
public bool guiShow = true; 

void Start() { 
    MoveStart(); 
    BulletStart(); 
} 
private void MoveStart() 
{ 
    speed = defualtSpdPlayer; 
} 
private void BulletStart() 
{ 
    if(ReloadTime > 1) 
    { 
     Debug.LogError("The Reload Time Is Bigger 1"); 
    } 
} 


void OnGUI() 
{ 

    //Verables 
    float cvReloadingWidth = 150; 
    float cvReloadingHeight = 150; 
    float cvReloadngY = Screen.height - cvReloadingHeight; 
    float cvReloadngX = Screen.width/2 - 70; 
    //Rects 
    Rect cvReloadingImages = new Rect(cvReloadngX, cvReloadngY, cvReloadingWidth, cvReloadingHeight); 
    Rect cvReloadinglalReloadTime = new Rect(cvReloadngX + 65, cvReloadngY + 75, cvReloadingWidth, cvReloadingHeight); 
    Rect cvReloadinglalStatus = new Rect(cvReloadngX + 40, cvReloadngY + 50, cvReloadingWidth, cvReloadingHeight); 
    //Texts 
    //Values 
    //Texture 
    GUI.DrawTexture(cvReloadingImages, ImgBackground); 
    GUI.DrawTexture(cvReloadingImages, ImgWhiteGround); 
    //GUI.DrawTexture(cvReloadingImages, ImgReloading); 

    if (reloadFillAmont <= 0) 
    { 
     GUI.skin.label.normal.textColor = Color.green; 
     GUI.skin.label.fontSize = 25; 
     GUI.skin.label.fontStyle = FontStyle.Bold; 
     GUI.Label(cvReloadinglalStatus, status); 
     GUI.skin.label.fontSize = 15; 
     GUI.skin.label.fontStyle = FontStyle.Bold; 
     GUI.Label(cvReloadinglalReloadTime, ReloadTime.ToString()); 
    } 
    else 
    { 
     GUI.skin.label.normal.textColor = Color.red; 
     GUI.Label(cvReloadinglalStatus, status); 
     GUI.Label(cvReloadinglalReloadTime, reloadFillAmont.ToString()); 
    } 
    //GUI 



} 

void Update() { 
    Move(); 
    Shoot(); 
} 
private void Move() 
{ 
    //Move 
    if (transform.rotation.y < 180) 
    { 
     plsSpeed(); 
     transform.Translate(Input.GetAxis("BW") * speed * Time.deltaTime, 0, Input.GetAxis("FW") * speed * Time.deltaTime); 
    } 
    if (transform.rotation.y >= 180) 
    { 
     plsSpeed(); 
     transform.Translate(-Input.GetAxis("BW") * speed * Time.deltaTime, 0, -Input.GetAxis("FW") * speed * Time.deltaTime); 
    } 
} 
private void plsSpeed() 
{ 
    if (speed > maxPlayer) 
    { 
     speed = defualtSpdPlayer; 
    } 
    else 
     speed += plsSpd; 
} 
//Gun Shoot 
private void Shoot() 
{ 
    if (readyToShoot) 
    { 
     if (Input.GetMouseButton(0)) 
     { 
      Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation); 
      readyToShoot = false; 
      reload = ReloadTime; 
      status = "Reloading"; 
     } 
     status = "Ready"; 
    } 
    else 
    { 
     reloadFillAmont = reload * ReloadFillAmontX; 
     if (reload < 0) 
     { 
      readyToShoot = true; 
     }else 
     { 
      reload -= Time.deltaTime; 
      status = "Reloading"; 
     } 
    } 
} 

} 

什麼是在碰撞的問題?

回答

1
  1. 您正在使用transform.Translate(0, -Speed, 0);移動子彈,當你使用這個子彈是怎麼回事,無論移動任何障礙/力之外吧。爲了解決這個問題,將一個剛體添加到子彈中,並將該線更改爲GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed);
  2. 由於它的子彈可能是快速移動的物體,因此將碰撞檢測置於Rigidbody的Continuous上。

RigidbodyMenu