Search Unity

Question [Newbie help] Bring children with crouching character

Discussion in '2D' started by arkhtyi, Mar 28, 2023.

  1. arkhtyi

    arkhtyi

    Joined:
    Mar 20, 2018
    Posts:
    2
    Hello.
    Really new to 2D unity and I'm going through a lot of early learning pains right now.

    I'm currently trying to make a character crouch AND bring the childrens with the character.

    For the details, I have a 2D side scrolling character that holds/shoots a gun. I put Shoulder/Gunpoint as childrens for the character and calculate the angle for the gun and where to fire.

    I also made the character able to crouch by resizing the collider size and changing the character to a crouching sprite.

    The problem is when the character crouches, the Shoulder/Gunpoint doesn't come down with the character. I would expect any associated children to move down with the collider, but I guess that was a wrong assumption.

    So the question is, how do I make these children GameObjects come down with the crouched character? For now, I probably only need to make the Shoulder point to come down, but I also thought this would be a bigger problem when I expand more points for my character in the future(like when I add some attachpoint for clothes or other gear).
    Maybe I should take a different approach for the character?

    Thank you so much in advance.

    upload_2023-3-28_17-59-4.png upload_2023-3-28_17-59-16.png
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Child transforms don't follow the collider scale of their parent, but they will follow the scale of the parent transform. However, changing the y scale of the parent transform would make your character and its gun scale down and look smashed on the y axis. Scripting will solve your issue. Use a script's Update method to watch for when your character goes into crouched mode. When it does, have it move the gun's transform.position.y down to an appropriate level. Upon standing back up, move the gun back up. If you've got multiple objects that all need to move down the same distance you can store them all within one child transform that acts as a parent to them, making them each grandchildren of the main transform, so that you only have to move the one child transform down.
     
  3. arkhtyi

    arkhtyi

    Joined:
    Mar 20, 2018
    Posts:
    2
    Thank you so much. I guess there's no simple way to attach it to the collider. I'll just try changing the transform position individually.