Making a Spline Train Using Rigidbodies to Move | Unity C#

Поделиться
HTML-код
  • Опубликовано: 13 окт 2024

Комментарии • 42

  • @paridotgames
    @paridotgames  Год назад +4

    *UPDATE* : I would advise not coupling the carts the way I did in this video - rigidbodies and collisions happen to be very unreliable joints. Instead, use actual joints! I made a new video specifically going over this: ruclips.net/video/F2CWDjjyadM/видео.html

  • @TrentSterling
    @TrentSterling Год назад +3

    Hey I came across that same reddit thread before I found this video. Thank you for documenting how this works with example code! I plan to combine this with a hinge joint that has break force, for fun reasons

    • @paridotgames
      @paridotgames  Год назад +2

      Glad to hear it has been a bit helpful!
      I haven't tried hinges yet but when I revisit this I think it will be the next thing I do. The way I set up the collider links on this project are a bit unreliable haha. Let me know if you get them working! I'd like to hear how it goes.

  • @er6n10
    @er6n10 5 месяцев назад

    Great stuff! Saved me from a headache I have been having while using the SplineAnimator...

  • @j.j.maverick9252
    @j.j.maverick9252 Год назад +2

    awesome work! Thanks for the walk through of your approach. I’ve done a number of spline based projects and often run into problems converting the time 0…1 into a velocity that is constant on different length splines (calculating the length is mostly an iterative approximation). I’m going to dig through your example and that reddit thread to see if there’s a better way.

    • @paridotgames
      @paridotgames  Год назад +1

      Hopefully it's helpful! Sounds like you're animating the transform based on a lerp between the start and the end of the spline? If that's true, my example is pretty much throwing that whole method out of the window lol. I just find the nearest point on the spline, snap the transform to it and make it face the direction of the tangent of the current point, then reapply the velocity onto the rigid body from the last frame.
      Very "hacky" solution but it allows for the application of physical forces while still following the spline. If I'm understanding your problem properly, you could implement my solution here, but just set the velocity to a constant value - this will stay the same no matter the length of the spline.

    • @j.j.maverick9252
      @j.j.maverick9252 Год назад

      @@paridotgames superb, thanks for your suggestions. Your demo looks super-cool!

  • @jeakdataje
    @jeakdataje Год назад +9

    we can't see the entire code in the video, please share the code.

  • @ilikecakeandbiscuits
    @ilikecakeandbiscuits Год назад +3

    Great tutorial. I'm having a problem where the train follows the track in the centre of the room and not where the spline is. Have a feeling it's due to the global vs local position but not sure how to get it to snap to the track!

  • @boywithbrain7556
    @boywithbrain7556 6 месяцев назад +3

    How did you aligh your bodies to the spline? cause mine always work on the spline with an offset

  • @ianh3385
    @ianh3385 3 месяца назад

    Seems a bit expensive if you plan on having lots of trains, since they will all have to find the closest point on the spline each tick. Hybrid solution seems the best way if your eally dont feel like writing some code for drag and acceleration

  • @mr.wolfmaster2892
    @mr.wolfmaster2892 Месяц назад

    Why is the cart offset from the spline path?

  • @falconelsu9798
    @falconelsu9798 Год назад +2

    That's awesome

  • @Hazzel31337
    @Hazzel31337 Год назад +1

    nice tuuut tuuut, nice "tutorial" great explanation

  • @isnimblebucket
    @isnimblebucket Год назад

    GetNearestPoint returning a local space vector instead of global really screwed me up too (my transform was at 0.9, 0 ,0 so it was just slightly off), glad I'm not the only one!

    • @paridotgames
      @paridotgames  Год назад

      That was quite the frustrating realization lol

    • @Hozgen
      @Hozgen Год назад

      i wanna carry the gameobject conntected to spline container but the locals will not :(

  • @RiFTRiSiNG
    @RiFTRiSiNG Год назад

    Hi, great tutorial, perfect for what I'm trying to do. I don't understand how you have attached the train cars, using a joint or in script? It was not shown in the video. Also, what can I add to the script to make the spline work if it is not globally centered. Thank you

    • @paridotgames
      @paridotgames  Год назад +1

      It was probably easy to miss but I did cover it in the video - I essentially added collider hooks on the front and back of each cart, then interlinked them such that they'd act like a chain pulling each other along. This proved to be very bug ridden and slow though and I have since re-implemented it using configurable joints instead. I plan to make a follow up video for that next, so if you're having trouble figuring it out yourself stay tuned for that :)
      For the spline global position problem, I have not spent the time working on that so I couldn't explicitly help you there, but I would think adding the current global position of the spline to each point you're accessing as an offset would be a good place to start.
      Hopefully that helps and good luck!

    • @RiFTRiSiNG
      @RiFTRiSiNG Год назад

      @@paridotgames Thank you!

  • @tomtintenfisch8029
    @tomtintenfisch8029 Год назад +1

    do you know where to find good documentation on the spline tool? i cant find anything good

    • @paridotgames
      @paridotgames  Год назад +1

      Unity supports the spline package so they have a manual and API docs on their website docs.unity3d.com/Packages/com.unity.splines@2.0/manual/
      It's definitely helpful, but for me I found it much more useful to just open up the example projects that come with the package and step through the code to see exactly how they use the API. I was able to more easily envision what I needed to do with a bigger picture understanding of how the code works together.

    • @tomtintenfisch8029
      @tomtintenfisch8029 Год назад +2

      @@paridotgames thank you, subbed

  • @crafty_dan1362
    @crafty_dan1362 Год назад

    Would anyone have any ideas on how to create splits in the track that the train can choose between. I know you can can create "branches" with the spline tool but how would the play choose between which path to take? Thanks for your help!

  • @paulandersson3315
    @paulandersson3315 Год назад +2

    What Spline Addon do you use?

    • @paridotgames
      @paridotgames  Год назад +1

      I'm using the standard Unity supported spline package: docs.unity3d.com/Packages/com.unity.splines@1.0/manual/index.html
      You can install it from the Unity package manager

    • @paulandersson3315
      @paulandersson3315 Год назад

      @@paridotgames thx

  • @MegaXDBrasil
    @MegaXDBrasil Год назад

    Wtf very big

  • @emi574
    @emi574 9 месяцев назад

    Make bogies rotate instead of whole locomotive

  • @USSR-Lenin-Stalin-Forever
    @USSR-Lenin-Stalin-Forever Год назад +1

    Matrix4x4 localToWorldMatrix = track.transform.localToWorldMatrix;
    Matrix4x4 worldToLocalMatrix = track.transform.worldToLocalMatrix;
    float distance = SplineUtility.GetNearestPoint(track.Splines[indexSpline], worldToLocalMatrix.MultiplyPoint(transform.position), out Unity.Mathematics.float3 nearest, out float t, 2);

    nextWagon.transform.position = localToWorldMatrix.MultiplyPoint(nearest);

    • @Hozgen
      @Hozgen Год назад

      Is it for global? 🤔

    • @Hozgen
      @Hozgen Год назад +1

      thats fine worked :D

    • @laytonvicars4606
      @laytonvicars4606 6 месяцев назад

      Thank you very much!