Search Unity

Resolved 2D Melee attack point can't flip?

Discussion in '2D' started by unity_16867F29A6AFEE08643E, May 27, 2022.

  1. unity_16867F29A6AFEE08643E

    unity_16867F29A6AFEE08643E

    Joined:
    Mar 3, 2022
    Posts:
    18
    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...

    and have been trying to use this
    Code (CSharp):
    1.  attackPoint.position = new Vector2(-transform.position.x, transform.position.y);
    This is my script...(Simple attack script)
    Code (CSharp):
    1.         if (Input.GetMouseButtonDown(0) && coolDownTimer == 0)
    2.         {
    3.             anim.SetTrigger("isAttack");
    4.             attackSound.Play();
    5.  
    6.             Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
    7.             foreach (Collider2D enemy in hitEnemies)
    8.             {
    9.                 enemy.GetComponent<Enemys>().TakeDamage(attackDamage);
    10.                 Instantiate(HitEffect, transform.position + (Vector3.right * 1.1f), HitEffect.transform.rotation);
    11.                 hitSound.Play();
    12.             }
    13.  
    14.             coolDownTimer = coolDown;
    15.         }
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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.
     
  3. unity_16867F29A6AFEE08643E

    unity_16867F29A6AFEE08643E

    Joined:
    Mar 3, 2022
    Posts:
    18
    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
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Yeah try that, it should flip the AttackPoint object for you.
     
  5. unity_16867F29A6AFEE08643E

    unity_16867F29A6AFEE08643E

    Joined:
    Mar 3, 2022
    Posts:
    18
    Tried using this code
    Code (CSharp):
    1.         if (dirX > 0f)
    2.         {
    3.             state = MovementState.running;
    4.             transform.Rotate(0f, 0f, 0f);
    5.             //sprite.flipX = false;
    6.         }
    7.         if (dirX < 0f)
    8.         {
    9.             state = MovementState.running;
    10.             transform.Rotate(0f, 180f, 0f);
    11.             //sprite.flipX = true;
    12.         }
    But instead of rotating, it starts to blink left and right like this
     
    Asanyth likes this.
  6. unity_16867F29A6AFEE08643E

    unity_16867F29A6AFEE08643E

    Joined:
    Mar 3, 2022
    Posts:
    18
    And I'm realizing that the rotation is not 180 but -180 (when I'm facing left)
     
  7. unity_16867F29A6AFEE08643E

    unity_16867F29A6AFEE08643E

    Joined:
    Mar 3, 2022
    Posts:
    18
    [UPDATE] Fixed by using localscale vector3
    Code (CSharp):
    1.         Vector3 currentScale = gameObject.transform.localScale;
    2.         currentScale.x *= -1;
     
  8. unity_16867F29A6AFEE08643E

    unity_16867F29A6AFEE08643E

    Joined:
    Mar 3, 2022
    Posts:
    18
    Thank you for your Insight
     
    Unrighteouss likes this.
  9. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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.
     
  10. WebDevEd

    WebDevEd

    Joined:
    May 21, 2022
    Posts:
    1
    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:

    Code (CSharp):
    1. public class GrabThrow : MonoBehaviour {
    2.  
    3.     public bool grabbed;
    4.     RaycastHit2D hit;
    5.     public float distance=2f;
    6.     public Transform holdpoint;
    7.     public float throwforce;
    8.     public LayerMask notgrabbed;
    9.     public Transform grabDetect;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.    
    19.         if(Input.GetKeyDown(KeyCode.B))
    20.         {
    21.  
    22.             if(!grabbed)
    23.             {
    24.                 Physics2D.queriesStartInColliders=false;
    25.  
    26.             hit =    Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, distance);
    27.  
    28.                 if(hit.collider!=null && hit.collider.tag=="grabbable")
    29.                 {
    30.                     grabbed=true;
    31.  
    32.                 }
    33.  
    34.  
    35.                 //grab
    36.             }else if(!Physics2D.OverlapPoint(holdpoint.position,notgrabbed))
    37.             {
    38.                 grabbed=false;
    39.  
    40.                 if(hit.collider.gameObject.GetComponent<Rigidbody2D>()!=null)
    41.                 {
    42.  
    43.                     hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity= new Vector2(transform.localScale.x,1)*throwforce;
    44.                 }
    45.  
    46.  
    47.                 //throw
    48.             }
    49.  
    50.  
    51.         }
    52.  
    53.         if (grabbed)
    54.                         hit.collider.gameObject.transform.position = holdpoint.position;
    55.  
    56.  
    57.     }
    58.  
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

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