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