2016-08-13 54 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