Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make a floating object over another??

Discussion in 'Scripting' started by engelcj, Jun 22, 2015.

  1. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    How to make a floating object over another. I do not want to float on water but on a physical object, just like the skateboard of Subway Surfers.

    Someone please ??

     
  2. benarceneaux

    benarceneaux

    Joined:
    Dec 17, 2012
    Posts:
    21
    What exactly are you having issues with? Do you just not know where to even begin moving a object?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    They probably just fake it by putting the visual model higher than the collision model.

    Alternatively, cast a ray below your character and offset the gravity if you're getting too low.
     
  4. benarceneaux

    benarceneaux

    Joined:
    Dec 17, 2012
    Posts:
    21
    I was going to say something very similar to Grozzler, I'd almost bet that the character in the pictured game isn't even "moving" but the scenery is being moved down, or scrolled under the character somehow. Maybe not though.
     
  5. Cnc96

    Cnc96

    Joined:
    Dec 17, 2013
    Posts:
    57
    I have achieved a fake hovering effect by placing the model at a certain height above the ground, and then freezing the vertical movement of the object
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or just animate the mesh a distance above the origin of the mesh in the mesh editor.
     
  7. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @benarceneaux @GroZZleR @Cnc96 @LeftyRighty I know how to make the move, but not how to make it float, I have tried placing the object at a height from the floor and lock the vertical movement, but this only works if the entire field is the same hight, I need to float over any another object although this higher.
     
  8. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This is obviously rough as I built it in twenty minutes but it looks pretty convincing to me:


    It's just a CharacterController with a high step value and an offset visual model. The bobbing is done via a script that changes the offset visual model's local position.
     
    engelcj likes this.
  9. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @GroZZleR That looks great, if you do not mind, share me the script ??
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    There's no real code involved except moving the character forward which I'm sure you know how to do by now.

    The setup looks like this:


    And the "Model" GameObject inside the Player has its local transform set to (0, 0.5, 0) so it's higher than the CharacterController. It's quite simple and basic.
     
    engelcj and Adroytus like this.
  11. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @GroZZleR Ohhh!! thank you very much, you have helped me a lot :)