Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Sorry for the delay. Anyways I think the boxcast is still centered like I told you in the response above. If it makes the enemy blue when you have the mouse the opposite way from it then that is probably the issue. If it turns blue when you have the mouse 90deg / 270deg from it then the cast is rotated incorrectly. 

To solve the issue with the boxcast being in the center of the character instead of being aligned to the pivot, change the:

void FixedUpdate() { 
        RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, new Vector2(attackRangeX, attackRangeY), 0f, transform.right, 4f, whatIsEnemies);
into something like:

void FixedUpdate() {
        Vector2 offset = transform.right * attackRangeY / 2;
        RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position + offset, new Vector2(attackRangeX, attackRangeY), 0f, transform.right, 4f, whatIsEnemies);

That will (as I reaponded with before) offset the boxcast so that it is in front of the character instead of in the center of it. Hope that works :D

(1 edit)

Hey! Thank you very much for your help! The problem was in the wrong angle

RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, new Vector2(attackRangeX, attackRangeY), 0f, transform.right, 2f, whatIsEnemies);
private void OnDrawGizmosSelected() {
		float angle = Util.GetAngleTowardsCursor(transform.position);
        Quaternion rotation = Quaternion.Euler(new Vector3(0f, 0f, angle - 90f));
		Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
        Gizmos.DrawWireCube(Vector3.right * 2f, new Vector2(attackRangeX, attackRangeY));
    }
public static float GetAngleTowardsCursor(Vector3 pos) {
	Vector3 mousePos = Input.mousePosition;
	mousePos.z = 5.23f;
	Vector3 objectPos = Camera.main.WorldToScreenPoint(pos);
		mousePos.x = mousePos.x - objectPos.x;
		mousePos.y = mousePos.y - objectPos.y;

float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
	return angle;
		}

Happy to help!

as planned it will be a staff of wind :)