I have a lot of collisions in my game and most of them worked well at first. But I’m noticing a lot of incidents now, where either the Player, an enemy or a projectile just moves throught an object without interacting with it.
Here’s the script of an enemy for example:
using UnityEngine;
using System.Collections;
public class WarriorBScript : MonoBehaviour {
public int strenght;
public float mspeed;
public float direction;
public float dir1;
public float dir2;
public bool legendary = false;
WarriorRScript otherWarrior;
public GameObject corpse;
void Start () {
SetDirection ();
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "RedSoldier")
{
otherWarrior = other.GetComponent<WarriorRScript>();
if(strenght <= otherWarrior.strenght)
{
Instantiate (corpse,transform.position,Quaternion.identity);
Destroy (gameObject);
}
}
if(other.tag == "Attack" || other.tag == "AttackRed")
{
if(!legendary)
{
Instantiate (corpse,transform.position,Quaternion.identity);
Destroy (gameObject);
}
}
if(other.tag == "RedSpell")
{
if(!legendary)
{
MagierRScript.redexp --;
Instantiate (corpse,transform.position,Quaternion.identity);
Destroy (gameObject);
}
}
if(other.tag == "Structure" || other.tag == "BlueSoldier" || other.tag == "BlueMage")
{
dir1 = -dir1;
dir2 = -dir2;
}
}
void SetDirection()
{
direction= Random.Range(0,360) * Mathf.Deg2Rad;
dir1 = Mathf.Sin(direction);
dir2 = -Mathf.Cos(direction);
Invoke ("SetDirection",5);
}
// Update is called once per frame
void FixedUpdate () {
transform.Translate(dir1*mspeed/100,dir2*mspeed/100,0f);
}
}
The GameObject executing the script and the object tagged as Structure both have a rigidbody2d and a collider2d with onTrigger enabled.
I also tried moving the Trigger effect to a script on the structure but about every 3rd collision doesn’t get recognised.
using UnityEngine;
using System.Collections;
public class StructureScript : MonoBehaviour {
MagierBScript magierBScript;
MagierRScript magierRScript;
WarriorBScript warriorBScript;
WarriorRScript warriorRScript;
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "BlueMage")
{
magierBScript = other.GetComponent<MagierBScript>();
magierBScript.mspeed = - magierBScript.mspeed;
magierBScript.Invoke("revertMspeed",0.1f);
}
if(other.tag == "RedMage")
{
magierRScript = other.GetComponent<MagierRScript>();
magierRScript.mspeed = - magierRScript.mspeed;
magierRScript.Invoke("revertMspeed",0.1f);
}
if(other.tag == "BlueSoldier")
{
warriorBScript = other.GetComponent<WarriorBScript>();
warriorBScript.dir1 = - warriorBScript.dir1;
warriorBScript.dir2 = - warriorBScript.dir2;
}
if(other.tag == "RedSoldier")
{
warriorRScript = other.GetComponent<WarriorRScript>();
warriorRScript.dir1 = - warriorRScript.dir1;
warriorRScript.dir2 = - warriorRScript.dir2;
}
}
}
I’ve also read, that the rigidbody of nonmoving object starts sleeping after some time, and tried to prevent them from doing so with the following code, but the problem still occured,
using UnityEngine;
using System.Collections;
public class WakeUp : MonoBehaviour {
Rigidbody2D scriptBody;
void Start() {
scriptBody = gameObject.GetComponent<Rigidbody2D>();
}
void Update(){
if (scriptBody.IsSleeping())
scriptBody.WakeUp();
}
}
I really hope, that someone on here knows how to improve the consistency of the OnTriggerEnter2D or how to handle collisions otherwise.
Thank you for your time and for possible answers.