Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

2D Minecart game using splines help

Discussion in '2D' started by Bakuryu42, Jan 24, 2021.

  1. Bakuryu42


    Nov 15, 2012
    Me and my group have been trying to make a minecart game similar to the ones in the Donkey Kong Country series and was recommended to use splines to make the character follow the rail nicer. We have yet to figure out a way to both have a spline system for it to follow and have that same spline render the rail sprite.
    Assets we have used/tried:

    We have gotten our character to follow the splines, but getting it to follow the sprite shape is proving difficult as the scripts used for the other spline assets don't seem be compatible with it. Any suggestions would be appreciated. We tried using the assets that use meshes instead, which aren't ideal because we have to manually tile the sprite based on length and also we couldn't get the mesh collider to work with the road creator.

    Any suggestions would be deeply appreciated.
  2. Venkify


    Unity Technologies

    Apr 7, 2015
    @Bakuryu42 Please take a look at Sprinkler.cs in the SpriteShape Extras available from PackageManager. It has functionality to place an Object along a random location of SpriteShape. You can use it as reference. Let us know if that helps.
  3. Bakuryu42


    Nov 15, 2012
    This is a step in the right direction in terms of orienting our player to the spline, although we are not 100% what each part is doing exactly. We are out of practice math wise.

    That said getting it to move alone the spline has been an issue as well. We were trying to use slerp, but the movement always ended up wrong. Like it always seemed to loose steam before it got to the end instead of moving alone it like it does with some of the other path following scripts.

    Also apparently we aren't the only ones who were looking to do this
    Last edited: Feb 11, 2021
  4. Bakuryu42


    Nov 15, 2012
    Update: We made some progress on this using another library as a base. Our current problem is that when we go up and down slopes our offset for our character doesn't work.

    We are calculating the bottom center point like this
    Code (CSharp):
    1. public SpriteRenderer _playersSpriteRender;
    2. public Vector2 _spriteHeight;
    4. _spriteHeight = / _playersSpriteRender.sprite.pixelsPerUnit;
    Then when we go to move the character we offset the y value by the _spriteHeight.y. We know the rotation is obviously messing with how the offset is suppose to be but we where exactly we are going wrong. Here is the code we are using for the movement.

    Code (CSharp):
    1.         private IEnumerator MoveForwardRoutine()
    2.         {
    3.             _distanceTraveled = 0;
    4.             while (true)
    5.             {
    6.                 if (_character.characterState == McStates.Riding)
    7.                 {
    8.                     tracker.transform.position = transform.position;
    9.                     transform.position = _pathController.GetPointByDistance(_distanceTraveled, true);
    10.                     _distanceTraveled += _character._controller.Speed.x * Time.deltaTime;
    12.                     //Rotate the object towards the movement direction(current position to next position)
    14.                     Vector2 dir = (_pathController.GetPointByDistance(_distanceTraveled, true) - (Vector2)transform.position).normalized;
    15.                     float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    16.                     transform.position += new Vector3(0, _spriteHeight.y, 0);
    17.                     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    21.                 }
    22.                 yield return null;
    24.             }
    25.         }