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. Dismiss Notice

Question How can I write a "Runner" script which follows the road

Discussion in 'Scripting' started by mfatihbarut, May 9, 2022.

  1. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    Hi all,
    Let's say we a have a road like this


    and we want our Mobile player only moves his finger from left to right and right to left on the screen to run in this road.
    To make it easy we should write a script that;
    1- Understands the road's direction at that frame and change the player's direction according to the road then make it move (on player's forward)
    meanwhile a cinemachine camera should follow the player (but couldn't also find the best combination of cinemachine cam's body and aim, to follow a player like this)
    What do you say?
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Very general question, so my general answer would be: Check out Splines (as a general technique or as an asset in the assetstore). There are AssetStore packages that even handle the mesh generation of the road for you. Personally I wouldn't see any sense in coding all of that myself. Something like this: Curvy Splines 8 | Utilities Tools | Unity Asset Store
     
  3. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    thanks a lot for reminding those.
     
  4. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,058
    But the main problem is they only follow the spline they don't let free moves.
     
  5. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    You mean player movement? Thinking of the typical endless runner I'd
    1. Get the next spline position
    2. Get the next spline direction
    3. Have my player positioned at the spline position plus an offset to the left / right.
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    A simple way to do this is to have an empty object as the spline follower and then make your player a child of the spline follower. Then all you have to do is move the character left and right and spline follower will do the rest.

    Edit: By the way, I want to also recommend Surge:
    https://assetstore.unity.com/packages/tools/utilities/surge-107312#description
    I'm sure Curvy Splines is nice, but Surge is free and easy to use, and it sounds like you only need a basic spline package for what you are doing.