- Home /
How to make an object float in space
I'm creating game and I would like to make several cubes float as if they are in space. I have a rigid body on each of them and I turned off the gravity. They don't fall, but they don't float around either. I tried the 'is kinematic' option without any luck. Any ideas?
Answer by EvilSquid · Aug 18, 2013 at 02:42 PM
You might want to add a tiny bit of force in various positions. This will make it move a bit, depending on how much force is applied. This can be achieved by creating a script named something like "Float" for example. Then open your script and change where it says "Update()" to FixedUpdate().
I do not know how experienced you are at scripting, but to add a force to the object type this into FixedUpdate().
rigidbody.AddForce(0,1,1);
What this will do is create a force that pushes your object on the Y and Z axis with a velocity of 1. The layout is (x,y,z) which can be compared with the X, Y and Z axis indicator in the top right hand corner of your indicator.
Of course, this is just an example, the higher the number the more force is applied. If you want it to move the opposite direction on the same axis (eg. down instead of up) simply make the number negative. For example
rigidbody.AddForce(0,-5,0)
will push your object down with a velocity of 5.
Make sure you connect your script to the object you would like to move, such as the cube in this case. This is just one way, but you can get a much better feel for coding in Unity and know what I'm talking about, along with other ways of doing this by visiting the tutorials section of Unity.
I hope this helped!
Answer by YoungDeveloper · Aug 18, 2013 at 03:34 PM
Add a rigidbody and this script.
public float FloatStrenght;
public float RandomRotationStrenght;
void Update () {
transform.rigidbody.AddForce(Vector3.up *FloatStrenght);
transform.Rotate(RandomRotationStrenght,RandomRotationStrenght,RandomRotationStrenght);
}
While playing you can adjust float and random rotation strength in the inspector for the best results.
Thank you very much, but the compiler is saying at position 12,12 unexpected token: float
You must attach this to the object which must float in the air. I tested this code, the problem must be in your own code. By the way this is C# not JS, i believe that is why it gives the error.
I believe not. It now gives me this compiler error: At 1,14 A namespace can only contain types and namespace declarations. Any ideas?
It's the exact code you posted. Nothing more, nothing less.
Answer by Sghosh72 · Jun 07, 2017 at 01:39 PM
Modifying from the code given above, you might want to make the floating object bob up and down in place. You can put an if condition, and enable gravity on the object.
Also be sure to spawn the object slightly above your intended floating position, because you don't want the object to develop too much of a velocity while falling down.
void FixedUpdate()
{
if (transform.position.y < floatingPosition)
{
Debug.Log("force being applied upwards to move object up");
transform.GetComponent<Rigidbody>().AddForce(Vector3.up * FloatStrength);
transform.Rotate(randomRotationStrength, randomRotationStrength, randomRotationStrength);
}
if (transform.position.y >= floatingPosition)
{
Debug.Log("force applied is less than the gravitational force so that the object comes down. Here mass of object is 2. ");
transform.GetComponent<Rigidbody>().AddForce(Vector3.up * 11.0f);
transform.Rotate(randomRotationStrength, randomRotationStrength, randomRotationStrength);
}
}
Answer by Rcocuzzo8 · Apr 23, 2016 at 10:48 AM
Turn Freeze Contraints On in the RigidBody for X,Y,and Z values. It will keep the object exactly where it starts at.