Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

I was able to move the pivot point, the rectangle rotates behind the cursor, but does not match the real raycast :(

RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, new Vector2(attackRangeX, attackRangeY), 0f, transform.right, whatIsEnemies);

private void OnDrawGizmosSelected() { Gizmos.color = Color.green; Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one); Gizmos.DrawWireCube(Vector2.right * 2f, new Vector2(attackRangeX, attackRangeY)); }

and the usual raycast works correctly, the line goes from the center of the character to a right behind the cursor

Debug.DrawRay(transform.position, transform.right * max_distance, Color.green); RaycastHit2D[] hitCenter = Physics2D.RaycastAll(transform.position, transform.right * max_distance, 4f, whatIsEnemies);

I understand that I don’t understand anything at all

It’s a bit too vague for me to understand, do you think you can post the full code and describe more exactly what it is supposed to do?

Actually it might just be the box cast that’s “wrong”. Right now it’s position is in the middle of the character, (the rotation aligns) so you need to make it reposition to work with the pivot. What this will do is make it so the boxcast the same as the gizmo (which I guess is what you want to do). Let me sketch it up.
That is how it looks right now. The arrow is the mouse, the dotted lines as the gizmos, the full lines are the rat cast and the point is the player. 

This is how we want to move the boxcast. In the same angle as the gizmo, half the length of the boxcast (that might be the width or height I’m not sure). 

So we want to take the angle of the cast, convert it to a vector, set that vectors magnitude to be half the length of the boxcast and then offset the boxcast by that vector. In code it would be something like:

Vector2 offset = new Vector2(cos(angle), sin(angle));
offset = offset * length / 2;
Vector2 positionOfCast = transform.position + offset;
(10 edits)

Hi! Here is my code http://p.tuduf.ru/UOtRtRc

If the enemy gets into the raycast, then it is clearly painted green, and with the cube otherwise, if I point the cursor at the enemy, it turns blue without entering the gizmo

public static Vector2 GetDirectionTowardsCursor(Transform baseTransform) {

var mouseWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);

return new Vector2(mouseWorld.x, mouseWorld.y) -

new Vector2(baseTransform.position.x, baseTransform.position.y);

}

public static Transform GetCharacterTransform()

{

return GetCharacter.transform;

}

(+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 :)