- Home /
Bullets sometimes don't work
Hi, I've followed a tutorial so I could get bullets on my game, since I actually wanted them to travel overTime and not an instant raycast, I didn't wanted gravity tho.
As I said in another question, my bullets are bigger than his, so I am using 3 raycasts instead of just one
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour {
public float speed = 100f;
public float lifeTime = 7f;
public GameObject decalHitWall;
[HideInInspector]
public Vector3 moveDirection;
[HideInInspector]
public float shortestSoFar;
[HideInInspector]
public Vector3 instantiatePoint;
[HideInInspector]
public Quaternion instantiateRotation;
[HideInInspector]
public bool foundHit = false;
public bool hasHitted = false;
public bool damagePlayer = false;
public int damage = 20;
public float widht = 0f;
void Awake () {
moveDirection = transform.forward * speed;
shortestSoFar = Mathf.Infinity;
foundHit = false;
}
void Update () {
if (!hasHitted){
transform.rotation = Quaternion.LookRotation(moveDirection);
RaycastHit[] hitsCenter;
hitsCenter = Physics.RaycastAll(transform.position, transform.forward, speed * Time.deltaTime);
foreach (var hit in hitsCenter){
float tempDistance = Vector3.Distance(transform.position, hit.point);
if(tempDistance < shortestSoFar){
instantiatePoint = hit.point;
instantiateRotation = Quaternion.LookRotation (hit.normal);
shortestSoFar = Vector3.Distance(transform.position, hit.point);
foundHit = true;
if(hit.transform.tag == "Enemy" && !damagePlayer){
hit.transform.parent.GetComponent<EnemyScript>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
GameObject.Find("Player").GetComponentInChildren<Player>().energy += 3;
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
if(hit.transform.tag == "Player" && damagePlayer){
hit.transform.GetComponentInChildren<Player>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
}
if(hit.transform.tag == "LevelPart"){
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
if(hit.transform.tag == "Spectator" && !damagePlayer){
hit.transform.parent.GetComponent<SpectatorScript>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
}
}
RaycastHit[] hitsLeft;
hitsLeft = Physics.RaycastAll(new Vector3 (transform.position.x - widht, transform.position.y, transform.position.z), transform.forward, speed * Time.deltaTime);
foreach (var hit in hitsLeft){
float tempDistance = Vector3.Distance(transform.position, hit.point);
if(tempDistance < shortestSoFar){
instantiatePoint = hit.point;
instantiateRotation = Quaternion.LookRotation (hit.normal);
shortestSoFar = Vector3.Distance(transform.position, hit.point);
foundHit = true;
if(hit.transform.tag == "Enemy" && !damagePlayer){
hit.transform.parent.GetComponent<EnemyScript>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
GameObject.Find("Player").GetComponentInChildren<Player>().energy += 3;
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
if(hit.transform.tag == "Player" && damagePlayer){
hit.transform.GetComponentInChildren<Player>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
}
if(hit.transform.tag == "LevelPart"){
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
if(hit.transform.tag == "Spectator" && !damagePlayer){
hit.transform.parent.GetComponent<SpectatorScript>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
}
}
RaycastHit[] hitsRight;
hitsRight = Physics.RaycastAll(new Vector3 (transform.position.x + widht, transform.position.y, transform.position.z), transform.forward, speed * Time.deltaTime);
foreach (var hit in hitsRight){
float tempDistance = Vector3.Distance(transform.position, hit.point);
if(tempDistance < shortestSoFar){
instantiatePoint = hit.point;
instantiateRotation = Quaternion.LookRotation (hit.normal);
shortestSoFar = Vector3.Distance(transform.position, hit.point);
foundHit = true;
if(hit.transform.tag == "Enemy" && !damagePlayer){
hit.transform.parent.GetComponent<EnemyScript>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
GameObject.Find("Player").GetComponentInChildren<Player>().energy += 3;
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
if(hit.transform.tag == "Player" && damagePlayer){
hit.transform.GetComponentInChildren<Player>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
}
if(hit.transform.tag == "LevelPart"){
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
if(hit.transform.tag == "Spectator" && !damagePlayer){
hit.transform.parent.GetComponent<SpectatorScript>().health -= damage;
shortestSoFar = Vector3.Distance(transform.position,hit.point);
Destroy (transform.root.gameObject);
GameObject spawnParticles = (GameObject)Instantiate (decalHitWall, instantiatePoint, instantiateRotation);
}
}
}
}
transform.position += moveDirection * Time.deltaTime;
lifeTime -= Time.deltaTime;
if(lifeTime <= 0f){
Destroy(gameObject);
}
}
}
Also, I made the code in c# where eteeki made it in js.
The bullets work fine most times but sometimes they just go throught enemies, I don't know what is wrong with the script, can soomeone help me?
Thaks in advance.
Edit: here's a webplayer version: https://dl.dropboxusercontent.com/u/151460612/webplayer/webplayer.html Ok this problem is actually almost never happenning, which is good, but at the same time it can possibily happen, which is bad, and why am I getting so many downvotes give me a reason please
why do you use [HideInInspector]rather then using private ins$$anonymous$$d of public? dose HideInInspector make it behave as a public but not be seen in the inspector?
It does exactly that, Jonathon. It helps prevent cluttered inspectors when you take the shortcut of not program$$anonymous$$g your properties/getters/setters...
$$anonymous$$ake your own script, and you will at least understand your mistake
Le Vailus so you downvoted me and just say to make my own script and don't even explain what is wrong?
Answer by RyanZimmerman87 · Jul 27, 2013 at 10:34 PM
First thing I would try is increasing the size of the collider you are hitting or even better slowing down the speed of the bullets. If it is only hitting some of the time chances are that your bullets are moving so fast that they may pass through the collider before Unity even detects them in a cycle.
I didn't read your question or code throughly enough but that would be my first guess. If that doesn't work I will try to edit my answer to go through other possibilities.
the bullet was already pretty slow compared to real bullets, because it's supposed to be a slow projectile, also, the collider that I'm trying to hit it's the enemies, sometimes it goes throught the smallest ones, but it sometimes can go right throught the biggest ones, which are actually pretty big, I'll upload my question with a webplayer, unfortunatly, the link may get broken, because dropboxs cancels links that are getting irregular traffic