Це відео не доступне.
Перепрошуємо.

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

Поділитися
Вставка
  • Опубліковано 14 сер 2024
  • Instead of animating a train along a spline, I wanted it to move using physical forces instead. I'll show you how I accomplished that in this video!
    If you'd like to support me making these videos, this is a great place to do so :)
    ko-fi.com/paridot
    The method for coupling carts done here has been vastly improved in a new video. Go check it out! • Using Configurable Joi...
    0:00 - Intro
    0:58 - Spline Package
    1:38 - Spline Component
    2:24 - Force Object on Path
    5:00 - Modelling Carts and Links
    5:53 - Outro
    Reddit post mentioned: / spline_based_rigidbody...
    GitHub Repository:
    github.com/Par...
    Join our Discord Server!
    / discord
    Wishlist our game RUIN!
    store.steampow...

КОМЕНТАРІ • 34

  • @er6n10
    @er6n10 3 місяці тому

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

  • @jeakdataje
    @jeakdataje Рік тому +8

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

  • @paridotgames
    @paridotgames  Рік тому +3

    *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: ua-cam.com/video/F2CWDjjyadM/v-deo.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.

  • @ianh3385
    @ianh3385 Місяць тому

    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

  • @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!

  • @boywithbrain7556
    @boywithbrain7556 4 місяці тому +1

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

  • @falconelsu9798
    @falconelsu9798 Рік тому +2

    That's awesome

  • @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!

  • @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 10 місяців тому

      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!

  • @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!

  • @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

  • @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 7 місяців тому

    Make bogies rotate instead of whole locomotive

  • @USSR-Lenin-Stalin-Forever
    @USSR-Lenin-Stalin-Forever 11 місяців тому +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 10 місяців тому

      Is it for global? 🤔

    • @Hozgen
      @Hozgen 10 місяців тому +1

      thats fine worked :D

    • @laytonvicars4606
      @laytonvicars4606 4 місяці тому

      Thank you very much!