Collisions not being detected at certain distance

Hey guys,

Fairly new to unity, and scripting in general. Working on my first project and having one issue i cant find and answer for.

Im making a top down shooter, i have most of the basic mechanics set up, the issue im having is that the projectiles the player shoots, while colliding correctly closer to the player, stop being detected if the enemy is at a distance from the player.

The distance is around 7 units, and only while the target is moving (collision appears to work normal when i make them stand still). I am also using the dontgothroughthings.js on the projectile.

The code is pretty rough

    //shoot.js
    var projectile : Rigidbody;
    var speed = 20;
    var reloadTime = 0.5;
    var targ : Transform;
    var force : int;
    
    private var nextFireTime = 0.0;
    
    function Start()
{
    	var go = GameObject.FindGameObjectWithTag("Player");
    	targ = go.transform;
}
    
    function FixedUpdate ()
{
    
    	if (Input.GetButton	("Fire1")&& Time.time > nextFireTime)
    		{
    			nextFireTime = Time.time + reloadTime;
    			targ.transform.Translate(-Vector3.forward * Time.deltaTime*force);
    			clone = Instantiate(projectile,transform.position, transform.rotation);
    			clone.velocity=transform.TransformDirection(Vector3(0,0,speed));
    		}
}



    //bullet.js
var dropOff : int = 4;
    
function Start ()
    
    {
    	Destroy(gameObject,dropOff);
    }
    
function OnCollisionEnter(collision : Collision)
    
    {
    	Debug.Log("i hit thing!");

    	if(collision.gameObject.tag == "Enemy")
    
        	{
        		Debug.Log("i die here right?");
        		Destroy(gameObject);
        	}
    }

//enemymovement.js
var waypoint : Transform[];
var speed : float=20;
private var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
var sight:int=10;

function Start(){
	var go = GameObject.FindGameObjectWithTag("Player");
	player = go.transform;
}

function Update () {
	if(currentWaypoint < waypoint.length){
		var target:Vector3 = waypoint[currentWaypoint].position;
		var moveDirection:Vector3 = target - transform.position;
		var distFromPlayer:Vector3 = player.position - transform.position;
		
	
		var velocity = rigidbody.velocity;
	
		if(moveDirection.magnitude<1){
			currentWaypoint++;
		}
		else if(distFromPlayer.magnitude<1000){
			target=player.position;
			velocity = moveDirection.normalized*speed;
			velocity= (player.position-transform.position).normalized*speed;
		
		}
	else{
		velocity = moveDirection.normalized*speed;
	}
	}
	else{
	if(distFromPlayer.magnitude<1000){
			velocity=Vector3.zero;
			target=player.position;
			velocity = moveDirection.normalized*speed;
		
		}
	
		if(loop){
			currentWaypoint = 0;
		}
		else{
		
			velocity = Vector3.zero;
		}
	}
	rigidbody.velocity=velocity;
	transform.LookAt(target);
}

Ok how about “Collision Detection” to “Continuous” or “Continuous Dynamic” on the bullet?

If that doesn’t do it, syclamoth’s suggestion of raycasts might be your best bet:

  1. Save your last position of the bullet in a variable
  2. Cast a ray from last position to current position, making sure that it’s sensitive to the layer of your enemy.
  3. If you get a hit, the hit collider’s GameObject is your bullet’s victim.

Example:
`


private var lastPosition:Vector3;

function Awake(){

lastPosition=transform.position;//so that it'll work the first Update() frame

}

function Update(){

var hit:RaycastHit;

//you might add a layer mask to the following to selectively hit enemy's layer:

if(Physics.Raycast(lastPosition,transform.position-lastPosition,hit,Vector3.Distance(lastPosition,transform.position))

	    &&hit.collider.gameObject.tag=="Enemy"){

	    Destroy(gameObject);

	    //the enemy is going to be hit.collider.gameObject.

}

lastPosition=transform.position;

}

`