Can you advise me with HIT detection please?

Hello guys. You helped me not long ago with my issue about projectile.

I am here once more for more help.
I picked up across internet some parts of projectile scripts and tutorials in order to create some basic projectile script. Problem comes when I am trying to detect collision of my high speed bullet with another object.

When I shoot slowly all my bullets are getting detected correctly but when I increase my rate of fire about 20% of my bullets fly trough my object without detection.

Here is my projectileScript

using UnityEngine;
using System.Collections;

public class ProjectileScript : MonoBehaviour {
	
	//Variables Start =========================
	
	public Vector3 muzzleSpeed;
 
    public float lifeTime;
 
    public bool ballisticOn;
    
	public int projectileDamage;
	
	private RaycastHit hit;
	
	private Vector3 previousPos;
	
	private Vector3 currentPos;
	
	private LayerMask layerMask = ~(1 << 8);
	
	//Variables End ===========================
	
	// Use this for initialization
	void Start ()
	{
		//This bassically sets lifeTime, after set lifeTime is out it will call destroyObject
		 if (lifeTime == 0)
		{
			lifeTime = 5;
			Invoke("BulletTimeToDestroy", lifeTime);
		}
	}
	
	// Update is called once per frame
	void Update ()
	{
		//This turns on ballistics, gravity is on if ballisticON is ON
		if (ballisticOn)
		{
			muzzleSpeed += Physics.gravity * Time.deltaTime;
		}
 		
		//This is speed of my projectile at muzzle
        if (muzzleSpeed == Vector3.zero)
		{
			return;
		}
        else
		{
			transform.position += muzzleSpeed * Time.deltaTime;
			
			transform.LookAt(transform.position + muzzleSpeed.normalized);
			
			Debug.DrawLine(transform.position, transform.position + muzzleSpeed.normalized, Color.red);
		}
		
		//This part should be about hit detection
		currentPos = transform.position;
		
		if (Physics.Linecast(previousPos, currentPos, out hit, layerMask) && (Time.deltaTime) > 0.001)
		{
			if (hit.collider.CompareTag("Damagable"))
			{
				Debug.Log("HIT!");
				DestroyObject(gameObject);
			}
		}
	}
	
	void FixedUpdate ()
	{
		previousPos = transform.position;
	}
	
	void BulletTimeToDestroy()
    {
        DestroyObject(gameObject);
    }
}

My projectile is little sphere and my “enemy” is big cylinder.
MuzzleSpeed is 500.

Does anybody know why is this detection so problematic? I fully understand almost every line I have in this script but still I am unable to locate the issue why it should behave differently with one projectile per a time or more projectiles per a time.

I am wondering if some kind of adjustment to collider of projetile or something would solve this… Any ideas?

Save the bullet as a prefab and make sure it has a rigidbody. Then, set collision detection to Continuous Dynamic. Also, under Interpolate, set that to Interpolate as well. This will make the bullets look nicer when flying through the air.

This is how I did shooting in my game:

public 	GameObject[] 	poolOfBullets;
public 	GameObject      bullet;
private Transform	myTransform;
public	Transform		forwardOfGunPos;
public 	float 			fireRate = 0.5F;
private int 		currentPosInBulletArray = 0;

void Awake()
	{
		poolOfBullets = new GameObject[30];
		for(int i = 0; i < 30; i++)
		{
			poolOfBullets *= (GameObject)Instantiate(bullet, new Vector3(400, 400, 400), Quaternion.identity);*

_ poolOfBullets*.SetActive(false);_
_
}_
_
myTransform = transform;_
_
}*_

void Shoot()
{
if(hasAmmo && !isReloading && Input.GetButton(“Fire1”) && Time.time > nextFire )
* {*
* nextFire = Time.time + fireRate;*

* if(currentPosInBulletArray < 30)*
* {*
* poolOfBullets[currentPosInBulletArray].SetActive(true);*
* }*
* else*
* {*
* currentPosInBulletArray = 0;*
* poolOfBullets[currentPosInBulletArray].SetActive(true);*
* }*
* poolOfBullets[currentPosInBulletArray].transform.position = forwardOfGunPos.position;*
* currentPosInBulletArray++;*
* }*
}
Then here is a script that is in every one of my bullets:
using UnityEngine;
using System.Collections;

public class BulletScript : MonoBehaviour {

* public float speed;*

* void Update ()*
* {*
_ transform.position += transform.forward.normalized * speed * Time.deltaTime;_
* }*

* void OnCollisionEnter()*
* {*
* gameObject.SetActive(false);*
* }*
}
If you need me to comment my code, let me know, otherwise it is good for you to learn and read other people’s code.

Modifying transform.position will cause the object to teleport, ignoring physics, right?

Use AddForce, or AddRelativeForce and use Rigidbody.MovePosition instead of transform.position.

But you shouldn’t need to add a force and move by position at the same time. Maybe unless you plan to shoot through walls.

Hello guys. So I tried to work on a script I was looking earlier (from Muzkaw) I took out some parts and modified the rest and my result is this.

//Variables Start =========================
	
    private float damage;
	
	public int velocity = 350;
	
	private Vector3 speed;
	
	private Vector3 previousPos;
	private Vector3 currentPos;
	private LayerMask layerMask = ~(1 << 8);
	private RaycastHit hit;
	public GameObject spawnpoint;
	
	private float time;
	private float lifeTime;
	
	private float line = 0.5f ;
	
	//Variables End ===========================
	
	void Start ()
	{		
		time = Time.time;
		speed = velocity * transform.forward;
	}
 
	 void Update() 
	{
		lifeTime = Time.time-time;
	
		if(lifeTime > 2.5)
		{
			Destroy(gameObject);
		}
		
		transform.position += transform.forward * Vector3.Magnitude(speed)*Time.deltaTime;
		
		//This if statement is here to avoid linecast from origin
		if(previousPos==Vector3.zero)
		{
			previousPos=spawnpoint.transform.position; 
		}
		
		currentPos = transform.position ;
		
		Debug.DrawLine(previousPos, currentPos, Color.red, line);
		
		if (Physics.Linecast(previousPos, currentPos, out hit, layerMask))
		{
			if(hit.collider.CompareTag("Damagable"))
			{
				Debug.Log ("Direct hit to Damagable object");
				Destroy(gameObject);
			}
		}
	}
	
	void FixedUpdate()
	{
		previousPos = transform.position ;
	}

I am able to detect 150 hits from 150 mouse clicks on same speed (500) which makes this for me as a good thing. Only thing left is to try to add gravity force which is not really there yet but I think I am on a good way.

There are still some parts that I don’t fully understand (as layerMask etc.) but it’s better than nothing. Still better than let one of you to make it for me.

Thank you for all your help and advice it helped me a lot in some cases to understand better to some parts of code. Thank you guys.