2016-08-13 10 views
0

の衝突でエラーが発生している:私は私が <p>スクリプト</p>弾丸で弾丸スクリプトOnCollisionEnter()メソッドを使用しています弾丸衝突の問題敵</p> <p>持つ弾丸シュート

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; 
    } 

} 
} 

弾丸が何も衝突しない

Player Scriptでメソッド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. に変更します。弾丸が速く動くオブジェクトなので、衝突検出をリジッドボディに継続的に置くことができます。

RigidbodyMenu

関連する問題