- Home /
2d Collision not working?
Hello, i have the problem that i have Several Objects with 2d Box-Collision and 2d Rigidbodies. But the Player and the test-objects aren't colliding. Also a 2d-ray doesn't collide with the objects.
Movement.js #pragma strict
var MovSpeed : float;
function Start () {
}
var damage : int = 5;
function Update () {
if (Input.GetKey(KeyCode.W)) {
transform.position.y = transform.position.y + MovSpeed;
}
else if (Input.GetKey(KeyCode.S)) {
transform.position.y = transform.position.y - MovSpeed;
}
if (Input.GetKey(KeyCode.D)) {
transform.position.x = transform.position.x + MovSpeed;
}
else if (Input.GetKey(KeyCode.A)) {
transform.position.x = transform.position.x - MovSpeed;
}
if (Input.GetKeyDown(KeyCode.Mouse0)) {
var ray = new Ray (GameObject.Find("Pistol").transform.position, transform.TransformDirection(Vector3(0,2,0)));
var hit : RaycastHit;
Debug.DrawRay(GameObject.Find("Pistol").transform.position,transform.TransformDirection(Vector3(0, 2, 0)), Color.white, 0.1, false);
if (Physics2D.Raycast(GameObject.Find("Pistol").transform.position, Vector2(2,0), 2) == true) {
print("Got it");
hit.collider.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
};
};
}
Enemy_test.js
#pragma strict
var hp : int;
function ApplyDamage(damage: int) {
hp = hp - damage;
if (hp <= 0) {
Destroy(this);
}
}
If you need more information, I attached the project. I hope that at least one will try to help me.
change your physics2d.raycast part to
if ((Physics2D.Raycast(GameObject.Find("Pistol").transform.position, new Vector2(1,1), 2)).collider)
Even when the Raycast/Enemy Collision is now working, the player/enemy don't collide. But thanks for helping me atleast at this problem
To Itaros, neither with $$anonymous$$inematic on or off, it doesn't collide with the things. And i reworked the raycast part a bit, but i get a object.reference error?
var collidern : RaycastHit; Debug.DrawRay(GameObject.Find("Pistol").transform.position,transform.TransformDirection(Vector3(0, 2, 0)), Color.white, 0.1, false);
if ((Physics2D.Raycast(GameObject.Find("Pistol").transform.position, new Vector2(1,1), 2)).collider) {
collidern.collider.transform.Send$$anonymous$$essage("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver); };