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 get smooth on-rails-movement?

Discussion in 'Editor & General Support' started by wannabeartist, Sep 28, 2009.

  1. wannabeartist

    wannabeartist

    Joined:
    Jun 20, 2009
    Posts:
    272
    Hi!

    I'm having some trouble getting a smooth movement of an object sliding "on rails".

    Forgive me for being so vague or overly general here - but I can't really be any more specific as the project is not yet public, but think of something like a train for now.

    I have set up a mesh collider for the surface where the object is supposed to slide on and the object itself has a box collider.

    The idea "works", but it's a rather horribly jagged movement and what I'm after a nice smooth slide.

    I have very little experience in tuning the physics for something like this, but I've tried different types of colliders and physic materials, so far with very little difference.

    I have also tried to change the slide surface geometry but I'm not sure if I should be adding (way) more polygons or quite the opposite?

    Any tips appreciated :)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There is some code in this thread that shows how to use a Bezier curve to plot a smooth path between points. There is also a script in this thread that shows how to use physics to move an object smoothly over a rough surface (it is actually used with a camera, but the same principle works with any object).

    Ask again if neither of these techniques is what you are looking for.
     
  3. wannabeartist

    wannabeartist

    Joined:
    Jun 20, 2009
    Posts:
    272
    Hello again,

    Do you mean, that I should not use actual collision detection to make the object "stay on rails", but rather use something like that script, or perhaps combine them?
    Because it's the collision detection part that is giving the jagged movement in my original project.


    It's a great script by the way, I played with it a bit and made one cube "chase" others in a scene - kind of like a magnet pushing another with same polarity :D
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,877
    Yes.

    Physics simulations in games today are not yet accurate or efficient enough to use them for making something stay on rails. Using a kind of spline based solution, like the one Andy linked to, is the way to go.

    Rune
     
  5. wannabeartist

    wannabeartist

    Joined:
    Jun 20, 2009
    Posts:
    272
    Alright, I kind of suspected that - thanks for clearing this out!
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I've just thought of something else that might help you out, but I'm not too sure...

    Is it possible to use, say, four SphereColliders instead of a BoxCollider for your object (putting them at the corners)? You might get a smoother result if you do this and also enable SmoothSphereCollisions on the MeshCollider.
     
  7. unithan

    unithan

    Joined:
    May 11, 2014
    Posts:
    8
    These links are no longer active. Were they moved?
     
    NioFox likes this.