Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mayorofspace · Aug 18, 2013 at 02:28 PM · rigidbodyobjectfloatcubespace

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image midomido · Aug 18, 2013 at 03:35 PM 0
Share

also you should use Random.Range to get a random force

avatar image
2

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mayorofspace · Aug 18, 2013 at 09:50 PM 0
Share

Thank you very much, but the compiler is saying at position 12,12 unexpected token: float

avatar image YoungDeveloper · Aug 19, 2013 at 10:08 AM 0
Share

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.

avatar image mayorofspace · Aug 25, 2013 at 12:37 PM 0
Share

I believe not. It now gives me this compiler error: At 1,14 A namespace can only contain types and namespace declarations. Any ideas?

avatar image YoungDeveloper · Aug 25, 2013 at 12:43 PM 0
Share

post your code

avatar image mayorofspace · Aug 25, 2013 at 01:04 PM 0
Share

It's the exact code you posted. Nothing more, nothing less.

Show more comments
avatar image
1

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);
             }
         }
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you keep spheres from floating into space? Rigidbody not working. 0 Answers

Making 3D Objects Stick On Other Objects Like A Grid? 2 Answers

How to change object colors? 1 Answer

Moving an object in 3D Space ~ Complexity 1 Answer

How to have a cube to fall on fixed rotation? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges