2D Melee attack point can't flip?

How can I flip the “AttackPoint” so I can attack enemies from both sides? Have been looking in Google but still I don’t get the concept…

https://vimeo.com/714531045

and have been trying to use this

 attackPoint.position = new Vector2(-transform.position.x, transform.position.y);

This is my script…(Simple attack script)

        if (Input.GetMouseButtonDown(0) && coolDownTimer == 0)
        {
            anim.SetTrigger("isAttack");
            attackSound.Play();

            Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
            foreach (Collider2D enemy in hitEnemies)
            {
                enemy.GetComponent<Enemys>().TakeDamage(attackDamage);
                Instantiate(HitEffect, transform.position + (Vector3.right * 1.1f), HitEffect.transform.rotation);
                hitSound.Play();
            }

            coolDownTimer = coolDown;
        }

Hey,

So normally I don’t see people flipping hitboxes in code, they flip the character and the hitbox inherits the scale or rotation of its parent (the player) automatically.

If you’re flipping your player by setting its x scale to -1, or y rotation to 180, then you shouldn’t need to do anything else. The AttackPoint is a child of the player, so it will inherit those changes automatically. If you’re flipping the player some other way, I would recommend setting its y rotation to 180 instead; that should solve your hitbox problem.

If you’d rather not do that, you just have to move the AttackPoint where you want it to be in code.

I just realise that i only flip x in the sprite renderer . So instead of flipping the sprite renderer, you recommend me to flip the Transform

Yeah try that, it should flip the AttackPoint object for you.

Tried using this code

        if (dirX > 0f)
        {
            state = MovementState.running;
            transform.Rotate(0f, 0f, 0f);
            //sprite.flipX = false;
        }
        if (dirX < 0f)
        {
            state = MovementState.running;
            transform.Rotate(0f, 180f, 0f);
            //sprite.flipX = true;
        }

But instead of rotating, it starts to blink left and right like this

https://vimeo.com/714682325

1 Like

And I’m realizing that the rotation is not 180 but -180 (when I’m facing left)

[UPDATE] Fixed by using localscale vector3

        Vector3 currentScale = gameObject.transform.localScale;
        currentScale.x *= -1;
1 Like

Thank you for your Insight

1 Like

That works, nice job figuring that out.

If you ever want to use rotation instead, don’t use transform.Rotate(), this will rotate the object by an amount every frame. What you want to do is set the object’s rotation. Rotation is a quaternion value, so it’s not as straightforward as setting position or scale.

Here are some examples of how to set an object’s rotation:
transform.rotation = Quaternion.Euler(0f, 180f, 0f);
transform.eulerAngles = new Vector3(0f, 180f, 0f);

180f and -180f are the exact same angle, so if you set the y rotation to 180f, you might see -180f in the game because it’s taking a quaternion angle and converting it to a vector3, but that’s fine; it’s the same angle.

2 Likes

apologies to tag on / hijack this insightful thread, i too am having a similar issue with my characters detection point when grabbing objects.

i am using a similar method for my grabDetection point and my player can only pickup an object on the right and drop it on the left or right but cannot detect and pick up objects on the left.

my player movement is define as degrees 0, 180 0, etc

the grab and throw script is below:

public class GrabThrow : MonoBehaviour {

    public bool grabbed;
    RaycastHit2D hit;
    public float distance=2f;
    public Transform holdpoint;
    public float throwforce;
    public LayerMask notgrabbed;
    public Transform grabDetect;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
        if(Input.GetKeyDown(KeyCode.B))
        {

            if(!grabbed)
            {
                Physics2D.queriesStartInColliders=false;

            hit =    Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, distance);

                if(hit.collider!=null && hit.collider.tag=="grabbable")
                {
                    grabbed=true;

                }


                //grab
            }else if(!Physics2D.OverlapPoint(holdpoint.position,notgrabbed))
            {
                grabbed=false;

                if(hit.collider.gameObject.GetComponent<Rigidbody2D>()!=null)
                {

                    hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity= new Vector2(transform.localScale.x,1)*throwforce;
                }


                //throw
            }


        }

        if (grabbed)
                        hit.collider.gameObject.transform.position = holdpoint.position;


    }

Just… please don’t necropost. Start your own thread. It’s FREE!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

If you have no idea, fix that first so you can report the problem correctly, or even solve it yourself. Here’s how:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.