Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Player compensating for shifting center of mass

Discussion in 'Physics' started by fdinsen, Jan 16, 2020.

  1. fdinsen

    fdinsen

    Joined:
    Jan 14, 2020
    Posts:
    2
    I'm looking to build a gameplay system where the player is using the keyboard to counteract an object with a shifting center of mass. Something akin to walking a tightrope, the player will lean to the right when the center of mass shifts to the left, and vice versa.

    My first idea was to have a script vary the Rigidbody2D.centerOfMass on my character over time and then have another, invisible gameobject with a rigidbody be controlled by the player. This completely messed with the physics though, and my character was flying all over the place. It also felt like a janky solution which was just begging to be broken. I feel like there must be some smarter way of doing this. Any suggestions on how to go about building/designing this setup are highly appreciated.

    I know this is less of a specific coding question and more of a design one, but I'm finding myself stuck at this problem. While I have experience coding, making games is still new to me, and I am having trouble finding the right way to think about the problems I'm working on, since the problem solving skills I use elsewhere don't feel like they apply here. So please bear with me.

    Thank you in advance,
    Frederik
     
  2. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    336
    Hello,

    Let me see if I understood right.

    Imaging the player is walking a tightrope, and the game throws balls to him.

    Actually, you could move the center of mass to lean the player and, because of this, to dodge the balls.
    This approach could work, but with a lot of fine tuning and try and failure. And if you make it work, it is quite non deterministic.

    So, it's better if you can avoid using physics for this.

    I think a better approach could be this:
    • A PlayerLeanController. Leaning is just a field that you modify by the input
    • This variable (leaning), updates an animator. This way you can see visually the player leaning
    • If you want to detect if the ball hits the player you could
      • You could have a box collider covering all the player. If the ball hits the player, you check against your leaning variable and decide if the ball hits the player or not
     
  3. fdinsen

    fdinsen

    Joined:
    Jan 14, 2020
    Posts:
    2
    Ah, no not exactly. I wasn't sure how to phrase it, so I guess it ended up pretty unspecific. Sorry about that.

    The goal isn't to dodge things, but to stay upright. If the player doesn't compensate for the shifting center of mass (which is just moving back and forth, left to right) the character will hit the ground, and the game is lost.

    I like the idea of not using physics, you're right it would take a lot of fine-tuning and so far it hasn't brought me anything but trouble. I hadn't thought of just using an animator to show the leaning. I could maybe just use a number which moves back and forth between two values, plus another number which the player is in control of moving within the same two values. Then if the difference between the two isn't big enough, that must mean the player isn't compensating enough, and the character will fall. That way it is all outside the physics. I will give this a try, thank you for your input, you were actually a lot of help!

    I hope you have a great day,
    Frederik