Objects not colliding
I'm quite new to c# and unity but wanted to give developing a simple 2D game a try. I have come to a major issue however. I want to first say this is NOT an issue with physics and rigid body collisions, I understand how they work (mostly) but this is about something different. I was working on the script that allowed my player to move, and tried to add a feature that would kill the player if they were hit my a falling 'enemy' object. However I cannot get the enemy and the player to collide. I can make them collide physically using rigid bodies, but they were not killing my player. I added a test script to see if the player and enemies were colliding all together, and it turns out that they were not. I played around for ages with box colliders, rigid bodies and the layer collision matrix, but nothing worked. What really stumped me was that my player could collide with another object, who's box collider was identical to the enemy's, but still couldn't collide with the enemy itself.
Here's the script I have on my player:
public class PlayerMovement : MonoBehaviour {
private Vector3 mousePosition;
public Vector3 fixedVelocity;
public float moveSpeed = 0.1f;
bool dead = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (dead)
return;
if (Input.GetMouseButton(0))
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
Vector3 position = transform.position;
position.y = -4;
position.z = -4;
transform.position = position;
}
}
void onCollisionEnter2D(Collision2D collision){
Debug.Log ("Player Triggered");
dead = true;
}
}
Answer by UnityCoach · Mar 27, 2017 at 10:04 AM
The message is OnCollisionEnter2D
, not onCollisionEnter2D
.
Also, you can disable the component, which will disable its Update calls. dead = true;
--> enabled = false;