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
2
Question by Somarl · Feb 13, 2014 at 09:24 PM · beginnerspherehover

How to make a ball float / hover

I have created a floor and a sphere and am trying to get the ball to hover (about 1.6meters) above the ground. The ball will need to have a rigidbody though and be subjected to gravity (in certain cases). I cant seem to replicate the slight "hover" effect for the ball where it goes up and down slightly.

I have seen a few other examples but either cant get them to work as they were in javascript and as much as i have tried i cant get them to work, or they are unpredictable and don't give the results i desired.

C# examples only please. Many thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by VesuvianPrime · Feb 13, 2014 at 10:08 PM

Hi Somarl

It sounds like you want the ball to Oscillate

You can achieve this motion in Unity by doing something like this:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     void Update()
     {
         this.transform.position = Vector3.up * Mathf.Cos(Time.time);
     }
 }


Comment
Add comment · Show 7 · 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 Somarl · Feb 14, 2014 at 10:11 AM 0
Share

Yes thanks. Oscillation is what im going for. This works great thank you. I cant however work out how to assign three values (speed of hovering, max height and $$anonymous$$ height) and make it oscillate between that. Also when i make this move around, will it be able to bounce off walls? (Sorry for the extra questions but i haven't been able to make it move around yet so i cant test this.)

avatar image VesuvianPrime · Feb 14, 2014 at 02:31 PM 2
Share

You can control the oscillation like this:

 float hoverHeight = (maxHeight + $$anonymous$$Height) / 2.0f;
 float hoverRange = maxHeight - $$anonymous$$Height;
 float hoverSpeed = 10.0f;
 
 this.transform.position = Vector3.up * hoverHeight + $$anonymous$$athf.cos(Time.time * hoverSpeed) * hoverRange;

The more i think about it, the more i think you may be better putting your physics components on one GameObject and adding your oscillating ball as a child. This way the physics and the oscillation wont interfere with each other.

avatar image DDeathlonger VesuvianPrime · Jun 21, 2018 at 09:57 AM 1
Share

Here's a more editor friendly version. :) ;)

Also, if you wanted something more "aware" of its surroundings, all you'd need to do is raycast up or down and if the distance from the raycast is less than a value, change the upper or lower limiters. :D

 using UnityEngine;
 
 public class Hover : $$anonymous$$onoBehaviour
 {
     [SerializeField] [Range(0, 100)] private float oscillationRate = 1;
     [SerializeField] [Range(0, 1)] private float oscillationRange = 1;
     [SerializeField] private float upperHeightLimit = 10;
     [SerializeField] private float lowerHeightLimit = 1;
     
     private void Update ()
     {
         transform.position = ClampHeight((Vector3.up * $$anonymous$$athf.Cos(Time.time * oscillationRate) * ClampRange(oscillationRange)) + transform.position);
     }
 
     private float ClampRange(float value)
     {
         if (transform.position.y > upperHeightLimit)
             upperHeightLimit = transform.position.y;
         if (transform.position.y < lowerHeightLimit)
             lowerHeightLimit = transform.position.y - lowerHeightLimit;
         if (upperHeightLimit < lowerHeightLimit)
             upperHeightLimit = lowerHeightLimit + 0.1f;
         if (lowerHeightLimit > upperHeightLimit)
             lowerHeightLimit = upperHeightLimit + 0.1f;
         if (value != ((upperHeightLimit + lowerHeightLimit) / 2) - 0.25f)
             value = ((upperHeightLimit + lowerHeightLimit) / 2) - 0.25f;
         if (value != value * oscillationRange)
             value *= oscillationRange;
 
         value *= 0.01f;
 
         return value;
     }
 
     private Vector3 ClampHeight(Vector3 value)
     {
         if (value.y < lowerHeightLimit)
             value.y = lowerHeightLimit;
         if (value.y > upperHeightLimit)
             value.y = upperHeightLimit;
         return value;
     }
 }
avatar image Bunny83 DDeathlonger · Jun 21, 2018 at 10:12 AM 1
Share

Uhm sorry but this term:

 ((normalizedRange + -normalizedRange) / 2.0f)

is just 0.0f and this term:

 (normalizedRange - -normalizedRange)

is just normalizedRange*2. Also note that you're adding the cosine to the position over time. So you effectively integrating the cosine method. Since the derivative and the integral of the cosine is just "-sine" you still get a sine wave. However due to integration your absolute y position can drift over time.


So your line of code can be simplified to:

 transform.position += Vector3.up * $$anonymous$$athf.Cos(Time.time * rate) * normalizedRange * 2;

This does exactly the same as your line of code ^^.

Show more comments
avatar image Somarl · Feb 15, 2014 at 07:13 PM 0
Share

Unfortunately i cant get this to work. I have the following script on the sphere

using UnityEngine; using System.Collections;

public class BallControl : $$anonymous$$onoBehaviour { public float maxHeight = 10f; public float $$anonymous$$Height = 5f;

 float hoverHeight = (maxHeight + $$anonymous$$Height) / 2.0f; //$$anonymous$$in and $$anonymous$$ax height are either not defined or if they are like above i get a field initialiser cannot reference the nonstatic field
 float hoverRange = maxHeight - $$anonymous$$Height;
 float hoverSpeed = 10.0f;

 void Update()
 {
     this.transform.position = Vector3.up * hoverHeight + $$anonymous$$athf.cos(Time.time * hoverSpeed) * hoverRange; //$$anonymous$$athf does not contain a definition of cos
 }

 void OnControllerColliderHit(ControllerColliderHit hit)
 {
     print("collision"); //This never works because collision is never detected no matter what.
 }
 

}

I am not sure though if $$anonymous$$onodevelop is being strange or not. I have never heard of $$anonymous$$athf not knowing what cos is. I however reinstalled unity, scrapped every project and started fresh and the same problem still occurs. I cant even run that script to see what values i want. In regards to the collision i think there is something wrong with the character controller as it never registers hits, only on trigger enter. There isnt a way to get a character controller to recognise collisions as yet in unity (otherwise the OnCollisionEnter bit would work, or OnControllerColliderHit(ControllerColliderHit hit) would work etc ) so i am going to work on building my own if i can.

avatar image blblopez · Oct 11, 2014 at 10:58 PM 0
Share

I believe capitalizing the "c" in $$anonymous$$athf.cos would solve the recognition issue.

However, then you'll probably run into the issue I did: operator "+" cannot be appluied to operands of type Vector3 and float. Alas, no solution to this at the moment, but someone on here must know a way around this!

avatar image
0

Answer by blblopez · Oct 12, 2014 at 12:50 AM

I believe capitalizing the "c" in Mathf.cos would solve the recognition issue.

However, then you'll probably run into the issue I did: operator "+" cannot be appluied to operands of type Vector3 and float. Alas, no solution to this at the moment, but someone on here must know a way around this!

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 MrSoad · Oct 11, 2014 at 11:56 PM

Take a look at this video tutorial. It is for floating on water, but remember that the water does not really exist and therefore it is exactly the same as floating on air(with the water plane render disabled) :

http://www.youtube.com/watch?v=mDtnT5fh7Ek

His code is short and works perfectly. You can adjust the water height value to make your sphere float at various heights. You can add a coroutine to add a regular slight adjustment to this for an extra bobbing effect.

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

23 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 avatar image avatar image

Related Questions

Make enemy follow and hit the player 3 Answers

Hovering Tank 2 Answers

Sorting variable names by their values. 2 Answers

First Person Controller. (Unity3) 1 Answer

Starting Unity/Programming from scratch 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