Jump to content
  • Advertisement
tastysushifish

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

This topic is 556 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

"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

Using mass to constrain the player to the world sounds like a really bad idea. Why can't you just limit the velocity and not apply forces after a certain theshold?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!