- Home /
Help with 2D topdown combat
Hello! I have been working on a little top down game with action combat where if you press to the left of the character, the player attacks left, if he presses to the right, attacks right and so on for above and below. I have 4 hitboxes to the left, right, below and above which I activate and deactivate with the animations for attack. My 4 direction attack animations are set up in a blend tree so that might also contribute to my problem. The problem I have is that even tho I press to the left of the character, the above or below hitbox (depending if i press left-above or left-below) activates as well and so on for all my directions. No matter which direction I press 2 colliders will be enabled and not one. I will also attach my mouse direction script in case that is the problem and I will also provide photos of the hitboxes that are enabling. Thanks in advance!
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = transform.position.z;
Vector3 attackDirection = (mousePosition - transform.position).normalized;
Debug.Log(attackDirection);
animator.SetFloat("MouseHorizontal", attackDirection.x);
animator.SetFloat("MouseVertical", attackDirection.y);![alt text][1]
[1]: /storage/temp/164216-untitled-1.png
Answer by Llama_w_2Ls · Jul 27, 2020 at 07:25 PM
Yes, the mouse clicking script that i sent earlier would cause various issues with this problem. This is because _when clicking to the left, your mouse click's y position may also detect that youre clicking above the player, causing two colliders to appear instead of the one you want. The solution that I would give is to add a deadzone. E.g:`public Camera cam; public GameObject player; public float Deadzone = 3f;
void Update()
{
if (Input.GetMouseButtonDown(0)) //if i left click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 Ray = ray.direction;
RaycastHit hit; //raycasts need to hit solid objects to detect a hit
if (Physics.Raycast(cam.transform.position, Ray, out hit)) //Shoot a raycast from my camera to where i clicked
{
if (hit.point.x < player.transform.position.x - Deadzone)
{
Debug.Log("Left");
}
else if (hit.point.x >= player.transform.position.x + Deadzone)
{
Debug.Log("Right");
}
if (hit.point.y < player.transform.position.y - Deadzone)
{
Debug.Log("Down");
}
else if (hit.point.y >= player.transform.position.y + Deadzone)
{
Debug.Log("Up");
}
}
}
}`
I found this to work well enough i guess, but if you want it to be perfect, you could create objects instead that would follow the player. When clicking on the objects, the click detected would never be both as you cant click on two at the same time. E.g:
public Camera cam;
public GameObject player;
public float Deadzone = 3f;
void Update()
{
if (Input.GetMouseButtonDown(0)) //if i left click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 Ray = ray.direction;
RaycastHit hit; //raycasts need to hit solid objects to detect a hit
if (Physics.Raycast(cam.transform.position, Ray, out hit)) //Shoot a raycast from my camera to where i clicked
{
if (hit.collider.gameObject.CompareTag("Left"))
{
Debug.Log("Left");
}
else if (hit.collider.gameObject.CompareTag("Right"))
{
Debug.Log("Right");
}
else if (hit.collider.gameObject.CompareTag("Up"))
{
Debug.Log("Up");
}
else if (hit.collider.gameObject.CompareTag("Down"))
{
Debug.Log("Down");
}
}
}
}
And the setup ingame would look something like this for 3D:
Your answer
Follow this Question
Related Questions
2D topdown combat collider vs OverlapBox etc... 1 Answer
[2D] Enable colliders around the player based on the direction of the mouse click 1 Answer
When should I use tilemap and game object? 1 Answer
How to animate topdown 2d water tiles? 1 Answer
How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers