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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I implement the quaternion system and character movement more simple

Discussion in '2D' started by ch070414, May 11, 2022.

  1. ch070414

    ch070414

    Joined:
    Jan 17, 2022
    Posts:
    11
    I'm writing to ask some questions about the implementation of quaternion.
    Currently I have a one set of animation that are looking right.
    For animation looking left, I decided to flip the character reflects to players z axis.

    upload_2022-5-11_20-13-1.png
    [idle - initial state]

    upload_2022-5-11_20-12-32.png
    when looking left.

    The problem is the quaternion. The bullet is supposed to be shoot along the mouse position, and rotated based on player position and mousePosition. this is actually awful situation because when I look left, the direction is opposite to the mouse Position (when looking right, no problem). How can I solve this problem?

    I came up with two ideas one is using left animation so that not to invert the quaternion. (but it requires repetitive labor).

    another idea is solve this problem in the quaternion... but I don't have idea how to resolve this.
    do you have any suggestion or better solution?

    // this is the code for rotating the bullet.
    Code (CSharp):
    1.  public void Shoot(Vector3 playerPos)
    2.     {
    3.         mouseClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.  
    5.         this.playerPos = playerPos;
    6.         Vector3 transPos = new Vector3(mouseClickedPos.x - playerPos.x, mouseClickedPos.y - playerPos.y, 0);
    7.  
    8.         float angle;
    9.         if (playerPos.y - mouseClickedPos.y > 0)
    10.         {
    11.             angle = -Vector3.Angle(new Vector3(1, 0, 0), transPos);
    12.         }
    13.  
    14.         else
    15.         {
    16.             angle = Vector3.Angle(new Vector3(1, 0, 0), transPos);
    17.         }
    18.  
    19.  
    20.         transform.Rotate(new Vector3(0, 0, 1), angle);
    21.         stop = false;
    22.     }

    upload_2022-5-11_20-19-6.png
    hierarchy of the bullet. it is in the empty container Wind Object. When rotating the bullet, the code access to the container (parent obj), because in wind sprite, I couldn't change the transform of the bullet because of the animation. it was fixed by the animation, so I'm looking for the way that looks like not flipping the quaternion.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Easiest way is to have a "hand" or "muzzle" point in the animated hierarchy that you flip over as well, via the animation process.

    Then the bullet doesn't care and takes its position and rotation from that object: regardless of whether you're facing left or right, it will always be correct.
     
  3. ch070414

    ch070414

    Joined:
    Jan 17, 2022
    Posts:
    11
    um.. what does the hand point mean? in animation process?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Whatever you are using to decide where the bullet goes, make sure that thing (usually an extra blank "hand" or "muzzle" child object) gets flipped by the animation in synchrony with the character flipping.