tastysushifish

Unity How to make the mass of an object increase the longer it's air born? (C#, Unity)

Recommended Posts

Using 3D physics how can I increase an object's mass the longer it's in the air.
what would be the best way to go about this? 
 

"for some context, I have a character who's movement is based off exerting force

in a set direction. currently, it can be pushed infinitely and I want to prevent

the player from pushing the character into the stratosphere. "
 
I am relatively new to coding and I am currently using Unity and scripting in C#.
 
Thanks for any help if possible.  :D 
 
Best wishes,
Thomas E.
 
 

Share this post


Link to post
Share on other sites

"for some context, I have a character who's movement is based off exerting force in a set direction. currently, it can be pushed infinitely and I want to prevent the player from pushing the character into the stratosphere. "

 
I don't think increasing the mass is going to have the effect you desire. Quoting from the Unity docs

"A common mistake is to assume that heavy objects fall faster than light ones. This is not true as the speed is dependent on gravity and drag."

Share this post


Link to post
Share on other sites

"for some context, I have a character who's movement is based off exerting force in a set direction. currently, it can be pushed infinitely and I want to prevent the player from pushing the character into the stratosphere. "


I don't think increasing the mass is going to have the effect you desire. Quoting from the Unity docs:

"A common mistake is to assume that heavy objects fall faster than light ones. This is not true as the speed is dependent on gravity and drag."

swiftcoder, but the movement is based on force, so it will have the desired effect. Velocity of fall will be the same, but pushing the object higher and higher will be more difficult.

Share this post


Link to post
Share on other sites
Yes, felipefdev that's the effect I'm going for, any recommendations or ideas how I would code such a thing, would i be better off basing it off of time in the air or Something else, I'm still a bit of a noob so any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
You know what would be more safe (physics engines tend to not like when you change certain properties like mass, often) than changing the rigid body mass constantly: apply less force the more time the rigid body is in the air.

You could reduce the force that is applied by a certain factor. For example, in each update, you could (pseudocode, since I don't know the particularities of Unity/C#):

object::update()
{
   if self::touchingGround() { // Check if the ground is right bellow
      self.applicableForce = 100.0; // My arbitrary max force
   } else {
      // If not touching ground, reduce applicable force to 75% each frame 
      self.applicableForce = self.applicableForce * 0.75 * deltaTime; // Adjust with the delta time
   }
   // Check input
   if getInput("up") {
      self::applyForce(directionVector * self.applicableForce) // Apply the force in the rigid body (self)
   }
}

Share this post


Link to post
Share on other sites

it will have to based on altitude, not time. otherwise they could push it sideways until it would no longer move.


you could just limit the altitude, or make the force a function of altitude.   F effective =  F in / alt. that sort of thing.

Share this post


Link to post
Share on other sites

both good ideas, I'll mess around and see what I can create with your suggestions,

I think I want a hard cutoff so for example... after 5 seconds in the air the player loses all control and falls in whatever direction they were previously accelerating.

 

Thanks again!

Share this post


Link to post
Share on other sites

it will have to based on altitude, not time. otherwise they could push it sideways until it would no longer move.


I was thinking about that, but if the terrain isn't flat, that can be flawed. Imagine if the player is on a mountain: it's at a high altitude, but technically it's still the ground. Using the altitude as parameter and raycast to determine the altitude isn't a good idea either. If there's a cliff, and the character just stepped out of the cliff, the effect will be quite drastic: the altitude will go from 0 to "MAX_ALTITUDE" (whatever the max altitude tested by the raycast is).

Share this post


Link to post
Share on other sites
The reason I thought limiting the mass would be interesting was because the force exerted could, at the right setting just bairly push the player up, like imagine a fat bird getting tired after a few flaps he can still fly upwards but only a tiny bit and at a significantly reduced rate. I figured increasing the mass would produce the desired effect. I hope this explanation doesn't make things more confusing.

I'm working on getting some images of my current build so you guys can see what I'm working with.

Share this post


Link to post
Share on other sites
If just being unable to push the player upwards into the stratosphere is the goal, a physically incorrect but probably plausible enough alternative might be a spring constraint to Earth (or a heavy object on the floor). Setting Max Distance to infinity so there's no cutoff, you have an ever-increasing downward force according to Hooke's Law (which will eventually equal/exceed the force that you are able to apply, thus there is an end to it). Or, you could do it with gameplay mechanics. Instead of not allowing the player be pushed into the stratosphere, [i]do allow[/i] that to happen. Only, above some height, show a "breath meter" decreasing, the higher up the faster, and if the player doesn't choose to lower the height any time soon, there's a sudden end to the game.

Share this post


Link to post
Share on other sites
Imagine if the player is on a mountain: it's at a high altitude, but technically it's still the ground.

 

by "altitude" i mean distance from the ground below the object to the object. not from sea level or some other arbitrary value, such as y=0 in a left hand 3D system.

 

 

raycast to determine the altitude

 

altitude is usually determined by subtracting the object y from the ground y returned by the heightmap.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites

Imagine if the player is on a mountain: it's at a high altitude, but technically it's still the ground.

by "altitude" i mean distance from the ground below the object to the object. not from sea level or some other arbitrary value, such as y=0 in a left hand 3D system.
Why, though? It's perfectly OK if you have a "high altitude" while standing on a mountain.
The stratosphere starts anywhere from 10km altitude to 18km altitude (depending on whether you're closer to the equator or the poles). A high mountain might be, for example, 8611 or even 8848 meters high, so you are indeed darn close to entering the stratosphere. You need breathing support as well.

So... is it really wrong to consider standing on that mountain that "high up" and prevent the player from going much higher? I think not. I think that's just how it should be.

Share this post


Link to post
Share on other sites

 

"for some context, I have a character who's movement is based off exerting force in a set direction.

 

perhaps a better explanation of the desired gameplay might help. 

 

 

Sure, no problem!

here's a better explanation of the gameplay.

As I mentioned in the original post, the player's movement is based on the force exerted in a set direction.

I made this work by creating a circular ring of trigger colliders that were scripted to apply force in the direction of the

player object when clicked on.  (see image below.) 

gallery_247866_1237_25075.png
Here is a link to a video of the set up working in the Unity editor.
 
 
please let me know if you need me to specify anything else. 
Thanks!

Share this post


Link to post
Share on other sites
Changing the force by reducing it gradually (like i mentioned in my post) or setting the force to 0 after S seconds sounds perfect for this case. Other undesired effect of changing the mass is that it will change the momentum, and will make the collision response between the character and other objects to vary according with the mass of the character.

Share this post


Link to post
Share on other sites

Changing the force by reducing it gradually (like i mentioned in my post) or setting the force to 0 after S seconds sounds perfect for this case. Other undesired effect of changing the mass is that it will change the momentum, and will make the collision response between the character and other objects to vary according with the mass of the character.

 

That seems like the best solution, Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Forum Statistics

    • Total Topics
      628293
    • Total Posts
      2981870
  • Similar Content

    • By CpawsMusic
      Hey there! My name is Cpaws and I compose music, sound effects and overall sound design for video games and film. I'm looking to work on a horror game mainly for fun and as an addition to my portfolio. I've used Unity & Wwise before for audio implementation.
      If you're interested in working together, don't hesitate to contact me at cpawsmusic@gmail.com
      Here's a few demo reels of my past projects: 
      Here's some snippets of some quick game music I've composed: https://soundcloud.com/cpawsmusic/sets/cpaws-video-game-film-music
      Here's my portfolio/website: https://CpawsMusic.com/
      E-mail: cpawsmusic@gmail.com
    • By muhamad rabee
      My first mobile game made by unity

      iphone: https://itunes.apple.com/us/app/aa-countdown/id1314223584?ls=1&mt=8

      android: https://play.google.com/store/apps/details?id=com.mr.AACountDown

      I appreciate every suggestion
    • By Simplepg
      https://play.google.com/store/apps/details?id=simple.gplay.GamesInABox
    • By Simplepg
      https://play.google.com/store/apps/details?id=simple.gplay.GamesInABox
    • By ForgedInteractive


      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! We're a team of artists, animators, programmers, level designers, writers, composers, producers, and other creative minds. We want to make games that you, the modern gamer want to play! We hope to build a community that enjoys our games as much as we love creating them. With your feedback and support we will be able to achieve that.

      About the Game
      GAME NAME is a fun, action-packed army builder with unique characters, challenges and engaging levels. Set forth on an adventure to protect friends, family and countrymen from new adversaries. Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.

      Previous Work by Team
      Although we are working towards our first game as a team, our team members themselves have past experience in the industry.
      This includes members who have worked on titles including:
      Final Fantasy Kingsglaive, FIFA, Xcom 2 and Civilization.

      Who are we looking for? 3D Modellers Concept Artists Marketing Specialists Level Designer

      What do we expect? Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer? Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      Contact
      If you are interested in knowing more or joining. Please email or PM us on Skype. Myself or Colin will reply to you within 48 hours.

      E-mail: Recruitment@ForgedInteractive.com
      Skype: ForgedInteractive

      Regards,
      David and Colin

      Follow us on:

      Facebook: https://www.facebook.com/ForgedInteractive/
      Twitter: @ForgedInteract
      Youtube: https://www.youtube.com/channel/UCpK..._as=subscriber
      Reddit: https://www.reddit.com/user/Forged_Interactive/
  • Popular Now