- Home /
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);
}
I don't suppose that the distance after which the enemy isn't being collision-detected is, say, 20 speed * 4 seconds = 80 units' distance? :)
If not, this code looks fine, so try to narrow it down by setting up your test enemy at various distances and deter$$anonymous$$e exactly how far away it is when it loses collision detection, pause the editor at that point and exa$$anonymous$$e both the projectile and enemy in the inspector in debug mode.
If the answer doesn't become clear, post the information here along with any other scripts that might be running on any relevant game objects.
lol that was the very first thing i checked, but alas no, when the collision isnt being detected its quite clear that the projectile is passing through the target and carries on past it.
I will try and get exact numbers for the distances, and post more info.
Basically, rigidbody collisions are unreliable at high speeds and for small objects. See 'bullet through paper'. Try using raycasts between the bullet's positions every frame ins$$anonymous$$d.
thanks for the tip, any direction you can point me for an example or tutorial in doing this, raycasting is probably the thing in understand least at this point
Answer by blitzen · Dec 07, 2011 at 05:56 PM
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:
Save your last position of the bullet in a variable
Cast a ray from last position to current position, making sure that it's sensitive to the layer of your enemy.
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;
}
Continuous Dynamic just created a whole other lot of issues (enemies would just stop in place after being hit??) trying this raycast thing, got "BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float, UnityEngine.RaycastHit)' was found." trying to work through it myself obviously but thought id put it up here too.
Whoops, I wrote that in a rush and it was untested. I just corrected it in the post above; try again.
Also, note that the bullet now no longer requires a collider, and neither the bullet nor enemy require a rigidbody. The enemy now just needs a collider and the "Enemy" tag.
that seems to work a treat, thanks for all the help. I also found my own solution still using rigidbody collision, changing the projectile script to an addforce rather then a transform movement seemed to remove the issue.
Your answer
Follow this Question
Related Questions
Fast object on collider not working 1 Answer
Why are my projectiles facing the wrong way? 1 Answer
How to fire projectile in direction character is facing? 3 Answers
ai help: fire shots 1 Answer
How to delay time reset on collision? 2 Answers