- Home /
Enemy collision in 2D top down
We (my friends and I) are trying to make our player take damage when he collides with the enemies, but needless to say we are having some difficulties. The player stops when colliding with the enemy but the health doesn't go down. Please help?
Note that we are beginners and haven't attempted a project of this size before.
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public GUIText healthText;
public GUIText dieText;
private int health;
public float forceSpeed = 0.01f;
public float speed = 5f;
void Start ()
{
health = 12;
SetHealthText ();
dieText.text = "";
}
void FixedUpdate ()
{
if(Input.GetKey ("s"))
{
transform.Translate(-Vector2.up * speed * Time.deltaTime);
}
if (Input.GetKey ("w"))
{
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if (Input.GetKey ("a"))
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey ("d"))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, moveVertical);
rigidbody2D.AddForce(movement * forceSpeed);
}
void OnCollosionEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
other.gameObject.SetActive(false);
health = health -1;
SetHealthText ();
}
}
void SetHealthText ()
{
healthText.text = "health" + health.ToString();
if(health == 0)
{
dieText.text = "YOU DIE!";
}
}
}
Not answering your question, but, i'll give some tips to boost your code and get more performance..
Put all the Input.Get$$anonymous$$ey(..) code inside a function, and after you set the translate according to what key was pressed, you just return, doing it, you won't check the other keys in that Update.
Something like:
void FixedUpdate()
{
Translate$$anonymous$$e();
/* your code.. */
}
private void Translate$$anonymous$$e()
{
float t = speed * Time.deltaTime;
if (Input.Get$$anonymous$$ey("w"))
transform.Translate(Vector2.up * t); return;
if (Input.Get$$anonymous$$ey("s"))
transform.Translate(-Vector2.up * t); return;
if (Input.Get$$anonymous$$ey("d"))
transform.Translate(Vector2.right * t); return;
if (Input.Get$$anonymous$$ey("a"))
transform.Translate(-Vector2.right * t); return;
}
@woodoo - Transform.Translate() is a void, so you cannot return it. You could return the vector used in the Translate() and then Translate() in the caller.
@robertbu - I was just trying to not use another line of code, i would need to open and close a block.. I thought that you could return void in a void function, considering that it returns void :)
The return in this case was just a shortcut to leave the function after translating, cuz there's no need to check other inputs in the current Update..
But if it is not possible to return void, I'll edit the previous comment.
Answer by robertbu · Jan 14, 2014 at 04:04 PM
I spot one problem. You are using the 3D version to detect collisions. For 2D, use OnCollisionEnter2D():
void OnCollosionEnter2D(Collision2D coll)
Answer by Pangamini · Jan 14, 2014 at 06:23 PM
The answer by robertbu is correct. However, there might be more problems at the same time:
I haven't tried Box2D implementation in Unity yet, but what i can say now is that you are using transform.translate for a rigid body, which in some physics engines, if you translate the body manually, it won't produce correct collisions. To correctly move a rigidbody you should use forces, and velocity for kinematic bodies (that's said in Box2D docs, thouugh using velocity in non-kinematic bodies should work too)
Also, you didn't mention having a rigidbody or a collider (2D versions) on your GameObject, so make sure both are there... OnCollosionEnter2D is a callback of physics engine
Thank you all for answering so quickly!
We have a Rigidbody2D component on the player, and a 2D Box Collider (we are currently just testing with cubes to get the code working). On the "enemy" we also have a 2D Box Collider.
We tried the OnCollisionEnter2D, but the player still went through the object without triggering it.
Update
We transferred the code onto a new scene where we only used sprites, and this time it worked. So we are guessing there must have been a problem with the default 2D boxes.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I get character to always face mouse? 2 Answers
2d ragdoll, where to start? 1 Answer
Introducing joystick to Unity? 0 Answers