- Home /
2D collider problems
Hello, I have got a problem with 2D collididers. I don't know why but Unity doesn't detect colliders. I am working on 2d game and i am trying to do the attack by player by throwing a spear and I want it to deal damage to enemy what it is in collision with it. Here is the code:
{ public float playerSpeed = 4f; public float playerPosX = 0; public float playerPosY = 0; public float swordRotateSpeed = 0.5f; private bool attack = false;
public GameObject player;
public GameObject kopija;
void Start()
{
}
void FixedUpdate()
{
Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
GetComponent<Rigidbody2D>().velocity = targetVelocity * playerSpeed;
}
// Update is called once per frame
void Update()
{
StartCoroutine(PlayerSavePos());
if(Input.GetKey(KeyCode.Mouse0))
{
attack = true;
}
if (attack==true)
{
//sword.transform.Rotate(Vector2.right, swordRotateSpeed * Time.deltaTime);
kopija.transform.Translate(Vector2.up * Time.deltaTime);
}
}
IEnumerator PlayerSavePos()
{
yield return new WaitForSeconds(5.0f);
playerPosX = player.transform.position.x;
PlayerPrefs.SetFloat("PlayerX", playerPosX);
PlayerPrefs.Save();
playerPosY = player.transform.position.y;
PlayerPrefs.SetFloat("PlayerY", playerPosY);
PlayerPrefs.Save();
}
void Awake()
{
if (PlayerPrefs.HasKey("PlayerX"))
{
playerPosX = PlayerPrefs.GetFloat("PlayerX");
}
if (PlayerPrefs.HasKey("PlayerY"))
{
playerPosY = PlayerPrefs.GetFloat("PlayerY");
}
}
I tried different types of colliders and i tried to play a little bit with a Rigidbody 2d settings and nothing is working. I am using Unity 2018.2.10f1. Please if anyone could help, then help me.
Answer by RatMoleRat · Oct 11, 2018 at 03:56 PM
A list of basic things to check:
If you want collision to register through script:
You need an OnCollisionEnter2D (if a collider isn't set to trigger) or OnTriggerEnter2D (if the collider is set to trigger) function in a script attached to the object that has the collider
In any case:
You need some kind of 2D collider on both objects, not set to trigger on either of them
You need a dynamic rigidbody2D on (at least) one object
Another thing that I noticed that is quite possibly an issue is that you are translating kopija (is that the weapon?) rather than moving it with a rigidbody. Depending on the scale, it might be teleporting through the enemy collider, rather than actually hitting it.
It might work better for you to use a Raycast in the direction of the spear, to check if the enemy would get hit. For example (note, I haven't tested this):
private static void CheckIfSpearHit (Vector2 direction) { //pass in direction of the attack
int range = 5; //or however far the weapon can reach
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, range);
//check if the raycast hit something
if (hit.collider.gameObject.name == "monster") { //or whatever its name would be
//do whatever you want here. maybe access script attached to enemy to decrease health
}
}
Your answer
Follow this Question
Related Questions
Particle Collision / Trigger not being reported to On_xx Event 1 Answer
Problem with collision with two colliders simultaneously in Unity 2D 1 Answer
Collision Enter one of the objects ? 1 Answer
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
2D:I have a movementController script on my character but it doesn't move forward 0 Answers