我在子弹射击时发生碰撞错误
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;
}
}
}
子弹没有碰撞任何东西
子弹在对象之外,因为我在播放器脚本中使用了 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";
}
}
}
}
碰撞有什么问题?
- 您正在使用
transform.Translate(0, -Speed, 0);
来移动子弹,当您使用它时,子弹将会移动,而不管它外面是否有任何 obstacle/force。要解决此问题,请向子弹添加刚体并将该行更改为 GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed);
- 因为它是一颗子弹,所以它可能是一个快速移动的物体,所以将刚体上的碰撞检测设置为连续。
我遇到了子弹碰撞敌人的问题
我在 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;
}
}
}
子弹没有碰撞任何东西
子弹在对象之外,因为我在播放器脚本中使用了 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";
}
}
}
}
碰撞有什么问题?
- 您正在使用
transform.Translate(0, -Speed, 0);
来移动子弹,当您使用它时,子弹将会移动,而不管它外面是否有任何 obstacle/force。要解决此问题,请向子弹添加刚体并将该行更改为GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed);
- 因为它是一颗子弹,所以它可能是一个快速移动的物体,所以将刚体上的碰撞检测设置为连续。