# 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 on other sites
swiftcoder    18437

"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 on other sites
felipefsdev    360

"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 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 on other sites
felipefsdev    360
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 on other sites
Norman Barrows    7179

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 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 on other sites

i'd say altitude too, a weird poor-man's way might be to

density += velocity.z * factor * dt;

at each step.

##### Share on other sites
felipefsdev    360

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 on other sites
cmac    275

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 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 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 on other sites
Norman Barrows    7179
Posted (edited)
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 on other sites
Norman Barrows    7179

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

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 on other sites
felipefsdev    360
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 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!

## 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

• ### Similar Content

•
Hi, I would like to have an opinion on what I can improve and change in my first published game.
The game was made on Unity3D.
Thank you.

• You are a US Army soldier, and one day an unknown enemy attacked your base. Now your task is to survive and protect the base. There are a lot of weapons in your arsenal. Survive this nightmare at any cost.

• Hi,
I am composer and sound designer that is trying to learn some codeing  Could You help me and tell me what should i add to change volume to 1.0F when enter trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioPlay : MonoBehaviour {

public AudioClip strings_1;
AudioSource audioSource;

// Use this for initialization
void Start () {
audioSource = GetComponent<AudioSource> ();
audioSource.PlayOneShot (strings_1, 0.0F );   - when game starts i want it to start but with 0 volume
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if(other.gameObject.tag == "slotviolin")
{
Debug.Log ("coilder works");

// how can i change volume to 1.0F and if it's possible how to make it smooth ?

}
}
}

Thanks

• Hold out as long as possible in the "Arena of Death." Only your skill and the correct tactics of combat will help you survive in the Arena. Nine types of weapons are at your disposal, but remember that cartridges are limited and they need to be spent wisely. You must prioritize the position and the type of weapons in order to achieve victory.   Awaiting you:   - 9 types of weapons - Dynamic gameplay - Different types of opponents, with their own characteristics and weaknesses - Increasing complexity   Download https://falcoware.com/DeadArena.php

• Hello guys
We are introducing our game Jump Jump And Fall
In this game you are help the cute box to jump every plain and collecting coin.
Every 5 coin collected, there is a new challenging plain.
Don't let the cute box fall down !
=: Gameplay video :=