• Advertisement

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
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
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, do allow 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

"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. 

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


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By 3dmodelerguy
      So I am building a turn based rogue-like (think CDDA). The game is going to have a very large map (up to 1000's x 1000's) however to alleviate most of that I obviously can't render everything so there will just be render a certain radius around the player and just load in and out data as the player moves.
      The next major system I am prototyping is making interactive tiles destructible and pretty much everything will be destructible besides basic landscape (cars, doors, windows, structures, etc. will be destructible)
      While I am only rendering a certain amount of tiles around the player, I want to keep the amount of colliders active at one time to be as small as possible for performance and currently the tilemap tool I use automatically merges colliders together.
      So instead of creating a separate colliders for each of these tiles and having the destructible behavior tied to that object (which my tilemap tool would allow me to do) I was thinking that I would store an array of all the X and Y locations for the interactive tilemap layer and let the tilemap manage the colliders. 
      Then when I hit a collider on the interactive tilemap layer, instead of of getting the behavior for how to deal with the destruction for that tile from that game object, I would pull it from the array I mentioned earlier based on the tile I attempt to interact with which I already have.
      Does this sound like a good approach? Any other recommendations would be welcomed.
    • By NDraskovic
      Hey guys,
      I have a really weird problem. I'm trying to get some data from a REST service. I'm using the following code:
       
      private void GetTheScores() { UnityWebRequest GetCommand = UnityWebRequest.Get(url); UnityWebRequestAsyncOperation operation = GetCommand.SendWebRequest(); if (!operation.webRequest.isNetworkError) { ResultsContainer rez = JsonUtility.FromJson<ResultsContainer>(operation.webRequest.downloadHandler.text); Debug.Log("Text: " + operation.webRequest.downloadHandler.text); } } The problem is that when I'm in Unity's editor, the request doesn't return anything (operation.webRequest.downloadHandler.text is empty, the Debug.Log command just prints "Text: "), but when I enter the debug mode and insert a breakpoint on that line, then it returns the text properly. Does anyone have an idea why is this happening?
      The real problem I'm trying to solve is that when I receive the text, I can't get the data from the JSON. The markup is really simple:
      [{"id":1,"name":"Player1"},{"id":2,"name":"Player2"}] and I have an object that should accept that data:
      [System.Serializable] public class ResultScript { public int id; public string name; } There is also a class that should accept the array of these objects (which the JSON is returning):
      [System.Serializable] public class ResultsContainer { public ResultScript[] results; } But when I run the code (in the debug mode, to get any result) I get an error: ArgumentException: JSON must represent an object type. I've googled it but none of the proposed solutions work for me.
      Also (regardless if I'm in the debug mode or not) when I try to do some string operations like removing or adding characters to the GET result, the functions return an empty string as a result
      Can you help me with any of these problems?
      Thank you
    • By nihitori
      The Emotional Music Vol. I pack focuses on beautiful and esoteric orchestral music, capable of creating truly emotive and intimate moods. It features detailed chamber strings, cello and piano as the main instruments, resulting in a subtle and elegant sound never before heard in video game royalty-free music assets.

      The pack includes 5 original tracks, as well as a total of 47 loops based on these tracks (long loops for simple use and short loops for custom / complex music layering).

      Unity Asset Store link: https://www.assetstore.unity3d.com/en/#!/content/107032
      Unreal Engine Marketplace link: https://www.unrealengine.com/marketplace/emotional-music-vol-i

      A 15 seconds preview of each main track is available on Soundcloud:
       
    • By RoKabium Games
      Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama
    • By eldwin11929
      We're looking for programmers for our project.
      Our project is being made in Unity
      Requirements:
      -Skills in Unity
      -C#
      -Javascript
      -Node.js
      We're looking for programmers who can perform a variety of functions on our project.
      Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
      We have a GDD to work off of, and a Lead Programmer you would work under.
      Assignments may include:
      -Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
      -Assembling already created monsters/characters with existing or non-existing code.
      -Creating VFX
      -Assembling already created environment models
      If interested, please contact: eldwin11929@yahoo.com
      This project is unpaid, but with royalties.
       
      ---
      Additional Project Info:
      Summary:
      Bassetune Reapers is a Player-verus-Player, competitive dungeon crawler. This basically takes on aspects of dungeon crawling, but with a more aggressive setting. Players will have the option to play as the "dungeon-crawlers" (called the 'Knights', or "Knight Class", in-game) or as the "dungeon" itself (literally called the 'Bosses', or "Boss Class", in-game). What this means is that players can choose to play as the people invading the dungeon, or as the dungeon-holders themselves.
      Key Features:
      -Intense, fast-paced combat
      -Multiple skills, weapons, and ways to play the game
      -Tons of different Bosses, Minibosses, creatures and traps to utilize throughout the dungeon
      -Multiple unique environments
      -Interesting, detailed lore behind both the game and world
      -Intricate RPG system
      -Ladder and ranking system
      -Lots of customization for both classes s of customization for both classes
  • Advertisement