- Home /
Mesh Collider Won't Work After Running Another Collider
I really don't know any other way to explain this but here's whats happening. I'm working on a 2D Space Shooter and my spaceship/spacecraft works fine when it set to a box collider. I'm able to generate the shield and everything and when the shield get's destroyed (after 3 collisions from an enemy object) this spaceship continues to get hit...
However my problem is,
Changing the spaceship from box collider to mesh collider (to make it more fair because the ship isn't a square and it can get hit before actually being touched) everything works fine until the shield get destroyed. After the shield is gone the enemies just passes through my ship. I'll post up my three scripts if it can help. (I did not paste the full code due to making it such a long post, instead I posted the parts I'm trying to figure out or what is being used)
// Player Script
public int numberOfShields = 3; // Set the number of shields our player gets to use public GameObject shieldMesh; // Load shield mesh public bool shieldOn =false;
void Update () { // Store input variables float transV = Input.GetAxis("Vertical") playerSpeedVertical Time.deltaTime; // Use to store vertical movement float transH = Input.GetAxis("Horizontal") playerSpeedHorizontal Time.deltaTime; // Use to store horizonal movement
// Move player based on input transform.Translate(transH, transV, 0); // X to move left and right. Y to move up and down
// Create a bullet if(Input.GetKeyDown("space")) { if (DefaultShot == true) { Instantiate(projectile, socketProjectileDefault.position, socketProjectileDefault.rotation); audio.Play(); } }
// Create a sheild if (Input.GetKeyDown(shieldKeyInput)) { if (shieldOn == false) { GameObject clone = Instantiate(shieldMesh, transform.position, transform.rotation) as GameObject; clone.transform.parent = gameObject.transform; shieldOn = true; } }
// Shield Script
public int shieldStrength = 3;
void Update () {
if (shieldStrength <= 0) { Destroy(gameObject); } }
// Enemy Script
void OnTriggerEnter (Collider other) { if (other.gameObject.tag == "Player") { scriptPlayer playerScript = other.GetComponent(); playerScript.lives -= 1; playerScript.DoubleShot = false; playerScript.DefaultShot = true;
// Tell scene manager that we lost a life scriptSceneManager sceneScript = sceneManager.GetComponent(); sceneScript.SubtractLife();
if (explosion) { Instantiate(explosion, transform.position, transform.rotation); audio.clip = playerDamageAudio; audio.Play(); }
StartCoroutine(playerScript.PlayerDamage()); ResetEnermy(); }
if (other.gameObject.tag == "Shield") {
scriptShield shieldScript = other.GetComponent(); shieldScript.shieldStrength -= 1;
if (explosion) { Instantiate(explosion, transform.position, transform.rotation); audio.clip = shieldExplosionAudio; audio.Play(); }
ResetEnermy(); }
if (explosion) { RandomSpeedAndSize(); }
}
Answer by RodrigoSeVeN · Aug 10, 2012 at 02:26 PM
You could stick to what is working, and adapt it.
These games normally have smaller collision boxes, as if cutting a small piece of the wings, to be fair to the player in the same manner you said. You get to keep your working mechanics, and will not lose anything just because you don't have mesh precision colision. You could set the mesh collider to convex to see if you like the results and if it works like the box.
Also, you never use meshs that are too complex for collision. You make a raw shape mesh to use for collision instead. In case using a smaller box is not good enough, try moddeling a simpler mesh, as this don't seen to be a code problem(mesh collider is not as reliable as the other simples types of collider).
Thank you for the answer, I tried what you said with the convex and it didn't work so I ended up sticking with what I had and just added another shape collider to fit the size of the ship.
Answer by GamersFrenzy · Jul 15, 2014 at 04:10 AM
Not the script, add Rigidbody to both the Player and the Trigger Collider and check kinematic on both I also set Collision Detection to continuous That should help out.