Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to rotate "limbs" towards a player in 2D

Discussion in '2D' started by tobiasfjelds, Jun 26, 2023.

  1. tobiasfjelds

    tobiasfjelds

    Joined:
    Nov 25, 2021
    Posts:
    7
    So I have this ship, and this ship has two arms. Each arm is a series of children within children. Pretty much it goes like this:
    Body > Shoulder > Arm > Elbow > Arm > Hand
    Now, only Shoulder and Elbow are empty objects, and I want to rotate them in script to rotate the arms towards the player, as if the hands are trying to grab the player.
    However, it seems like the rotation is very wonky, and I can't seem to get it to work. The game is top-down, and the body of the ship rotates independently (mostly just follows player).

    I have genuinely no idea how to do this properly. Quaternions and rotations just break my mind each time, and I need some serious help. I feel like I've tried everything!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    You will need some kind of IK (inverse kinematics) along with rules about how to bend these things, eg, how to blend bending among the joints.

    Obviously anytime there is more than three segments ( a triangle of rigidity), there will be more than one solution to the resulting rotation.

    Either that or just make canned animations.
     
  3. tobiasfjelds

    tobiasfjelds

    Joined:
    Nov 25, 2021
    Posts:
    7

    Are these possible to do in 2D though?
    Because I've gotten the limbs to move, but it's just the precision that is way off
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Yes.
     
  5. tobiasfjelds

    tobiasfjelds

    Joined:
    Nov 25, 2021
    Posts:
    7
    I checked out a video on Inverse Kinematics, and I don't think it's necessarily the solution that I need.

    This isn't a problem with animation, it's moreso about making the arms rotate towards the player. I just need some code to do that, but I dont know what to write in the code

    I may also add that I've been able to do this exact thing in the game engine Construct 3 a few years back, so I would expect that it would be possible here if rotations weren't such a pain to deal with
     
  6. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    Without ik, max you can do is something like this. I don't know what your game is, so this is just an example for 2d top down angle calculations for you

    Black diamond is the ship, green blob is the upper arm and red blob is the lower arm/hand. You want the hand to point towards purple circle. You need to find the alpha angle. You can do it by:
    1- find the vector from tip of the upper arm or base of the lower arm to purple circle. That vector is x,y
    2- use Mathf.Atan2(y,x) to find the alpha angle. https://docs.unity3d.com/ScriptReference/Mathf.Atan2.html
    3- alpha is in radians. we "need" degrees to change the rotation of an object. We'll multiply alpha with rad2deg https://docs.unity3d.com/ScriptReference/Mathf.Rad2Deg.html
    4- now set the local rotation.z of lower arm to alpha*Mathf.Rad2Deg. You can test this by changing the rotation.Z value in the inspector. If you encounter errors on not being able to directly set the Z value of a rotation etc., search online please

    5- if you rotate green blob or the ship after setting the rotation of the red blob, you need to do these calculations again

    6- if the pivot point of the red blob isnt at the tip of the green blob, calculations may be wrong
    upload_2023-7-1_1-19-0.png