- Home /
[2D] Enable colliders around the player based on the direction of the mouse click
Hello there! I am working on a 2D topdown perspective roguelike and I was wondering how I could enable by code hitboxes around the player based on the mouse click. For example: if i press the mouse to the left of the player, the left hitbox shoul be enabled. If i press below the player, the down hitbox should be enabled and so on. Any help is much appreciated!
Answer by scroolerr · Jul 26, 2020 at 07:31 PM
Hey, i'm not a pro but i'll try to help you.
try this:
public Collider2D YourColliderNameHere
void Start()
{
YourColliderNameHere.SelfActive(false);
}
// Gets mouse button down
if(Input.GetMouseButtonDown(0)){
// Sets collider active to true
YourColliderNameHere.SelfActive(true);
}
// Gets mouse button up
if(Input.GetMouseButtonUp(0)){
//Sets collider active to false
YourColliderNameHere.SelfActive(false);
}
Oh sorry, didnt read that you wanted to disable and enable them based on ur mouse position.
You could try to read the mouse position on the screen and then if mousePos.x is higher than player.transform.position.x and ur receiving a mouse click, disable and enable the specific collider that you have.
Once again, sorry, I didnt read your whole question
Hey thanks for the ideea! @scroolerr That is basically the problem i have now. I will give you the code and maybe you can tell me what is wrong.
if (attackDirection.x > transform.position.x) hitBoxes[0].enabled = true;
else if( attackDirection.x < transform.position.x) hitBoxes[1].enabled = true;
else if(attackDirection.y > transform.position.y) hitBoxes[2].enabled = true;
else if(attackDirection.y < transform.position.y) hitBoxes[3].enabled = true;
The problem is that even if i press above the player it still activates my left or right collider based on which direction my mouse is closer to, left or right. I would really appreciate if you could help me out.
Your answer
Follow this Question
Related Questions
Help with 2D topdown combat 1 Answer
How to check if mouse is above or below player? 2 Answers
8-directional orientation, top down 2D, seperate from movement 0 Answers
2D top-down multiple sprite rotation 1 Answer
Touch Buttons 0 Answers