- Home /
Rigidbody Collider Issues
Ok i have a rocket launcher that shoots out a rocket... Thats not the problem it shoots fine. The rocket is suppose to collide with any objects in its path and explode... I have an imported terrain(because flash doesn't allow you to use the unity's terrain) that has a mesh collider. The rocket shot has a capsule collider. The rocket however collides with every other object except the Terrain... It collides occasionally but not very often and i want it to collide every time. Rocket Script:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
public class RocketExplode : MonoBehaviour {
public int timeOut;
public GameObject explosion;
public GameObject character;
private Vector3 position;
void Start () {
timeOut = 3;
character = GameObject.FindWithTag("Player");
Invoke("Kill", timeOut);
Physics.IgnoreCollision(character.collider, collider);
}
void FixedUpdate()
{
if(transform.position != position)
{
if (Physics.Linecast (transform.position, position))
{
Instantiate(explosion,transform.position,transform.rotation);
Kill();
}
position = transform.position;
}
}
void OnCollisionEnter(Collision collision)
{
ContactPoint contact = collision.contacts[0];
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
Instantiate (explosion, contact.point, rotation);
Kill();
}
void Kill()
{
ParticleEmitter emitter = GetComponentInChildren<ParticleEmitter>();
if(emitter)
emitter.emit = false;
transform.DetachChildren();
Destroy(gameObject);
}
}
Also on top of that the rocket doesn't destroy itself after it explodes... Any help is appreciated. Added in what syclamoth told me to do... did i do it wrong or something?
I have heard that setting the mesh collider to convex will work but the terrain is rather curvy(if ya know what i mean) and setting it to convex would make the collider boxy(not what i want.)
Try augmenting the rocket's collision detection with a simple raycast collider to catch anything that the capsule misses.
Are suggesting a Physics.OverlapSphere or something else?
No, I mean simply using Physics.Linecast between the previous position and the current position every FixedUpdate step. If it detects a collision, calculate reflection/damage/destruction as you see fit. It's not as accurate as rigidbody collision, but it's faster and more reliable, and for small objects or fast objects it doesn't make that big a difference anyway.
var position:Vector3; function Start() { position=transform.position; } function Update() { if(position!=transform.position)Debug.Log("Position has changed"); //make sure to cast before resetting position to transform.position position=transform.position; }
Answer by syclamoth · Mar 12, 2012 at 12:45 AM
Augment the discrete rigidbody physics engine with a simple linecast collider:
var lastPos : Vector3;
var hitLayers : LayerMask;
function Start()
{
lastPos = transform.position;
}
function FixedUpdate()
{
if(Physics.Raycast(lastPos, position, hitLayers))
// Make sure you use a layermask that excludes the bullet itself!
{
// POW!
}
lastPos = position;
}
Thanks dude your a savior this actually works! Thanks!