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 trouble using splines as a grind rail

Discussion in 'Editor & General Support' started by okaybj, Sep 25, 2023.

  1. okaybj

    okaybj

    Joined:
    Nov 15, 2018
    Posts:
    33
    Using the new splines package I've instantiated a bunch of colliders on to the spline with SplineInstantiate for my player to spherecast hit. This works but I'm now struggling with moving the player along the spline.

    using: (from the only spline package tut on all of youtube lol)
    distancePercentage += speed * Time.deltaTime / splineLength;
    Vector3 currentPosition = spline.EvaluatePosition(distancePercentage);
    transform.position = currentPosition;
    Vector3 nextPosition = spline.EvaluatePosition(distancePercentage + 0.05f);
    Vector3 direction = nextPosition - currentPosition + new Vector3(0,2.5f,0f); //to account for player height

    I can move along the spline but always only from the start until the end. I need to be able to start from wherever my spherecast hits.

    using:
    RaycastHit grindHit;
    if(Physics.SphereCast(transform.position, playerHeight * 0.5f, -transform.up, out grindHit, 2f, whatIsGrind))
    {
    closestNode = grindHit.collider.transform;
    }

    I can get the transform.position of where I should start from but I don't know how to factor this into the above code. And to be fair I would rather use some sort of rigidbody based movement.

    Also I dont know how to reference the SplineContainer property from a spherecast hit if the colliders I'm hitting are not a child of the spline..
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.

    Don't conflate all your problems into one. Work through tutorials for each of the parts:

    - find the nearest point on a given spline (will be dependent on what spline code you use)
    - attach object to that point on the spline
    - move along the spline by whatever mechanism you want

    Rigidbody physics are not going to follow a spline. You would need to take over and drive the Rigidbody yourself if you needed that. But first, solve the above issues and get it moving with a simple transform first.