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

Resolved [2D Top Down] How to ignore parent rotation ? My crosshair follow my player rotation animation

Discussion in 'Scripting' started by misterunsafe, Feb 17, 2021.

  1. misterunsafe

    misterunsafe

    Joined:
    Feb 15, 2021
    Posts:
    10
    Hello everyone !

    I'm working on my first project ! In my animation I rotated on the z axis my character so each time I move in a direction , my character is facing the right way.

    I have a crosshair moving with my mouse position and follow my character with a magnitude, but each time I move in a direction with my keyboard, my crosshair rotate along with my character.

    How do i ignore my parent rotation AND keep my parent transformation ? Do I need to change something in the animation inspector or in my script ?

    Or is there simply an other way ?

    I know that child inherits from their parents rotation/transformation, but I can't figure out how to keep my crosshair in the magnitude of my player and follow him without make him a child AND without breaking my script.

    Here a GIF to better understand (crosshair is in tiny red and has too many opacity in gif sorry boyz, but u can guess it easily). U can see my bullets going the right way so my aiming script is "working" but my sprite isn't at the right place !



    And here's the function I call in Update() in my script :
    Code (CSharp):
    1. private void AimAndShoot() {
    2.         Vector2 shootingDirection = new Vector2(aim.x, aim.y);
    3.  
    4.         if (aim.magnitude > 0.0f) {
    5.             crossHair.transform.localPosition = aim * 0.4f;
    6.             crossHair.SetActive(true);
    7.  
    8.             shootingDirection.Normalize();
    9.             if(endOfAiming) {
    10.                 GameObject ammo = Instantiate(ammoPrefab, transform.position, Quaternion.identity);
    11.                 ammo.GetComponent<Rigidbody2D>().velocity = shootingDirection * 3.0f;
    12.                 ammo.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
    13.                 Destroy(ammo, 2.0f);
    14.             }
    15.         } else {
    16.             crossHair.SetActive(false);
    17.          }
    18.     }
     
    Last edited: Feb 17, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    It's hard to say without seeing your full hierarchy setup, but perhaps line 5 above you just set
    .position
    rather than
    .localPosition
    ?
     
  3. misterunsafe

    misterunsafe

    Joined:
    Feb 15, 2021
    Posts:
    10
    Hello Kurt, i'v tried that already but I had this problem : crosshair doesn't rotate anymore BUT doesn't follow my character movement :/

    My Hierarchy is very simple I only have my player(parent)->crosshair(child)


    Also I'm completely new sorry if I didn't understood the question or don't get obvious thing !
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Oh then I think you need to do two things:

    1. set the .position (as I noted above)

    2. but set it to aim + your character's .position

    The way this differs from simply setting .localRotation is that it does not rotate by the heading.
     
  5. misterunsafe

    misterunsafe

    Joined:
    Feb 15, 2021
    Posts:
    10
    So I set my line like this for 1.

    crossHair.transform.position = aim * 0.4f;



    But I don't get what I need to do in the second part, can you give me a code example please ?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    I was thinking about:

    Code (csharp):
    1. crossHair.transform.position = aim * 0.4f + thePlayer.transform.position;
     
  7. misterunsafe

    misterunsafe

    Joined:
    Feb 15, 2021
    Posts:
    10
    It's working king, thank you so much, I can go to so sleep peacefully now :cool:
     
    Kurt-Dekker likes this.