簡體   English   中英

我在子彈射擊中發生碰撞時出錯

[英]I have error in collision on bullet shoot

我在子彈碰撞敵人時遇到問題

我在Bullet腳本中使用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;
    }

}
}

子彈不碰撞

項目符號超出了對象范圍,因為我在播放器腳本中使用了方法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. 您正在使用transform.Translate(0, -Speed, 0); 移動子彈,當您使用此子彈時,子彈會移動,而不管其外部有任何障礙物/力量。 要解決此問題,請在項目符號中添加一個GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed); ,然后將該行更改為GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed);
  2. 由於其可能是快速移動的物體,因此請將“碰撞檢測”設置為“剛體”上的“連續”。

剛體菜單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM