Hi!
I’ve got a little problem with Calculating a Collisions.I want my Game to calculate the difference in positions betwen Player and Every GameObjects with NPCEssentialsScript.The Player is for each A and D key moving 6.4 in the X axis and 10 for each W and S Key in Y axis.
public class PlayerEssentials : MonoBehaviour {
private bool collW;
private bool collS;
private bool collA;
private bool collD;
// ...
void OnCollisionEnter2D(Collision2D coll)
if(transform.position.y - NPCEssentials.tr.position.y == -10 && transform.position.x == NPCEssentials.tr.position.x) //W
{
print ("W");
collW = true;
}
else if(transform.position.y - NPCEssentials.tr.position.y == 10 && transform.position.x == NPCEssentials.tr.position.x)//S
{
print ("S");
collS = true;
}
else if(transform.position.x - NPCEssentials.tr.position.x == 6.4 && transform.position.y == NPCEssentials.tr.position.y)//A
{
print ("A");
collA = true;
}
else if(transform.position.x - NPCEssentials.tr.position.x == -6.4 && transform.position.y == NPCEssentials.tr.position.y)//D
{
print ("D");
collD = true;
}
}
}
void OnCollisionExit2D()
{
collW = false;
collS = false;
collA = false;
collD = false;
}
}
and the NPCEssentials.tr = transform(of the NPC).
When I’m running my game I’m getting NullReferenceException for each If in the code, but everyting is declared.