Making A Physics Based Character Controller In Unity (for Very Very Valet)

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.ย. 2024

ความคิดเห็น • 324

  • @mixandjam
    @mixandjam 3 ปีที่แล้ว +163

    This video is perfect! Thank you so much for sharing the knowledge!! Love it, super well explained and fun to watch!!

  • @_sophies
    @_sophies 2 ปีที่แล้ว +46

    Just wanted to say this is the best character controller tut that I've seen, and all the character controllers I've made since watching this have been based off of this.

  • @joshuastubblefield2559
    @joshuastubblefield2559 ปีที่แล้ว +16

    Thanks a lot for this video. It's great! Here's my version of vertical hovering after following along. I researched a bit on spring-damp mechanics and was able to define the behavior using frequency and a damp factor rather than spring strength and damp strength. This way, you set dampFactor to 1 and it will reach equilibrium without over shooting. Set dampFactor to 0 to get a spring that (ideally) never loses energy. dampFrequency defines how fast it reacts.
    using UnityEngine;
    public class Hover : MonoBehaviour
    {
    public float dampFactor = 1;
    public float dampFrequency = 15;
    public float hoverHeight = 1.5f;
    public float maxDistance = 2;
    public float castRadius = .5f;
    public Rigidbody rb;
    RaycastHit[] hits = new RaycastHit[10];
    private void Awake()
    {
    if (!rb)
    {
    Debug.LogError($"[{nameof(Hover)}] missing field RigidBody.");
    enabled = false;
    }
    }
    private void FixedUpdate()
    {
    ApplyHoverForce();
    }
    void ApplyHoverForce()
    {
    if (GroundCast(out RaycastHit hit))
    {
    Vector3 rayDirection = Vector3.down;
    float springDelta = GetSpringDelta(hit);
    float springStrength = SpringStrength(rb.mass, dampFrequency);
    float dampStrength = DampStrength(dampFactor, rb.mass, dampFrequency);
    float springSpeed = GetRelativeSpeedAlongDirection(rb, hit.rigidbody, rayDirection);
    Vector3 springForce = GetSpringForce(
    springDelta,
    springSpeed,
    springStrength,
    dampStrength,
    rayDirection);
    springForce -= Physics.gravity;
    rb.AddForce(springForce);
    if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(-springForce, hit.point);
    }
    }
    bool GroundCast(out RaycastHit hit)
    {
    int hitCount = Physics.SphereCastNonAlloc(
    transform.position,
    castRadius,
    -transform.up,
    hits,
    maxDistance);
    if (hitCount > 0)
    {
    for (int i = 0; i < hitCount; i++)
    {
    RaycastHit current = hits[i];
    if (current.rigidbody == rb) continue;
    hit = current;
    return true;
    }
    }
    hit = default;
    return false;
    }
    float GetSpringDelta(RaycastHit hit)
    {
    return hit.distance - (hoverHeight - castRadius);
    }
    static float GetRelativeSpeedAlongDirection(
    Rigidbody targetBody,
    Rigidbody frameBody,
    Vector3 direction)
    {
    Vector3 velocity = targetBody.velocity;
    Vector3 hitBodyVelocity = frameBody ? frameBody.velocity : default;
    float rayDirectionSpeed = Vector3.Dot(direction, velocity);
    float hitBodyRayDirectionSpeed = Vector3.Dot(direction, hitBodyVelocity);
    return rayDirectionSpeed - hitBodyRayDirectionSpeed;
    }
    static float SpringStrength(float mass, float frequency)
    {
    return frequency * frequency * mass;
    }
    static float DampStrength(float dampFactor, float mass, float frequency)
    {
    float criticalDampStrength = 2 * mass * frequency;
    return dampFactor * criticalDampStrength;
    }
    static Vector3 GetSpringForce(
    float springDelta,
    float springSpeed,
    float springStrength,
    float dampStrength,
    Vector3 direction)
    {
    float tension = springDelta * springStrength;
    float damp = springSpeed * dampStrength;
    float forceMagnitude = tension - damp;
    Vector3 force = direction * forceMagnitude;
    return force;
    }
    }

    • @diegorg64
      @diegorg64 2 วันที่ผ่านมา

      Thanks for your implementation of vertical hovering. I'm having some problems for movement on slopes. When I go up a slope, the character stays too close to the ground, and when I go down, it stays too high, causing the character to make small falls.
      My parameter values are:
      maxDistance = 1.5f
      hoverHeight = 1.2f
      dampFrequency = 15f
      dampFactor = 0.55f

  • @MythicLegionDev
    @MythicLegionDev 3 ปีที่แล้ว +27

    Awesome explanation and solutions, thank you for sharing! I've been developing a similar physics based character controller by trying to combine my favorite parts of a responsive platformer controller and a raycast vehicle controller so this is all super helpful!

  • @kerduslegend2644
    @kerduslegend2644 4 หลายเดือนก่อน +4

    man, this is such a hidden gem. i dont know why i dont know your channel earlier

  • @gabrielplourde6791
    @gabrielplourde6791 3 ปีที่แล้ว +24

    This was very intelligently designed and explained. So many subtle changes that really bring it all together to near perfection, I love it

  • @tdif3197
    @tdif3197 ปีที่แล้ว +11

    You guys have a great set of tutorials here. Glad to find another floating capsule user, it really is the way to go.

  • @AForceofWill
    @AForceofWill ปีที่แล้ว +1

    I have never thought of using floating capsules... this is so ingenius! I can't believe my solution to rough player controls was this simple. You are amazing sir!

  • @FlipYourLearning
    @FlipYourLearning 2 ปีที่แล้ว +6

    Nice video. The code is well written and although I had seen this approach in other tutorials none could explain it as well as you did.

  • @Foxyzier
    @Foxyzier ปีที่แล้ว +1

    Can't stress enough how grateful I am for this video, there really isn't enough explanations on physics based character controllers like that.

  • @monochroma3803
    @monochroma3803 2 ปีที่แล้ว +8

    i like how technical explanations are and art style is so cheerful and fun to watch ! good job !

  • @spacewizards9039
    @spacewizards9039 3 หลายเดือนก่อน

    Wow, very impressed. The game I'm working on uses (mostly) hover vehicles, so I was already using a setup similar to yours (floating "characters" with continually calculated strength and dampening forces). But you guys have really taken it to the next level with improvements that I hadn't thought of at all. Thanks!

  • @bubski3821
    @bubski3821 5 หลายเดือนก่อน

    this is one of the smartest custom controllers I've ever seen, so impressed with yall's work on this

  • @theDarkerSan
    @theDarkerSan 2 ปีที่แล้ว +1

    I combined this with ground normals movement (ground raycast hit normals plane projection ) and it turned out amazing! thanks again.

    • @mattsYT42
      @mattsYT42 ปีที่แล้ว

      could you please explain that in lamen's terms 😅

  • @simpathey
    @simpathey 3 ปีที่แล้ว +5

    This is so exciting! I am pumped for the game, and thanks for talking in depth about your physics based movement and even showing code. I will for sure give this a go in my next game!

  • @ali32bit42
    @ali32bit42 ปีที่แล้ว

    i was so lucky this studio exists cause i keep coming back to this as refrence for my own game.

  • @megasoniczxx
    @megasoniczxx 2 ปีที่แล้ว +1

    I still don't get completely all of it but this has definitely helped me get a firmer grasp on what makes a good 3D character controller. Really insightful video.

  • @RoffeDH
    @RoffeDH 2 ปีที่แล้ว +1

    Hey, just created this Git repository with the assets I've created based on your tutorial.
    Just wanted to say thanks as this is EXACTLY what I was looking for.

    • @munyunu
      @munyunu 2 ปีที่แล้ว +2

      Ahhh whereeee is the repo please?

  • @sutoreikyatto
    @sutoreikyatto ปีที่แล้ว

    I feel so lucky that this video exists since I've been struggling on implementing a physics based first person controller and this tutorial explains almost all the issues I've faced. Thank you so much!!

  • @yaderkunsama9454
    @yaderkunsama9454 2 ปีที่แล้ว

    I've been looking for an explanation, a hint on physics based movement for the past 3 months. This has really helped figure out how to do it, at least gave me a starting point. Thanks for the explanation!

  • @TrentSterling
    @TrentSterling 3 ปีที่แล้ว +5

    Great visualizations! Looking forward to implementing something similar!

  • @7DragonEyes7
    @7DragonEyes7 3 ปีที่แล้ว +6

    Really interesting. Release it on PC and I'll get myself a copy :)

  • @Sergeeeek
    @Sergeeeek 10 หลายเดือนก่อน

    Thanks for your tutorials!
    I implemented a first person controller with this approach and it feels great, can walk over any terrain including other rigid bodies, even moving platforms! When landing a jump it crouches slightly due to the spring which feels realistic and satisfying.
    Got your game on Switch, looking forward to play it with friends this weekend :)

    • @patrickbateman4641
      @patrickbateman4641 7 หลายเดือนก่อน

      im struggling to implement this for fps, could you give any pointers? ty

    • @Sergeeeek
      @Sergeeeek 7 หลายเดือนก่อน

      @@patrickbateman4641 well I think this video explains it pretty well, what are you having issues with?

  • @geminiseDigitalTwin
    @geminiseDigitalTwin 2 ปีที่แล้ว

    The hovering capsule mechanic is really clever. Finally found something that works for my project.
    Thanks for sharing.
    Amazing video. Great Explanation !!!

  • @ed_halley
    @ed_halley ปีที่แล้ว

    I got pretty much all of the controller stuff working except the jump force working. Because the character's on a spring and could be moving up or down at the time of the jump, I guess I need to do the "neededAccel" calculation to achieve a consistent starting jump velocity. And then also support the jump-hold to bring it up to a maximum velocity.

  • @supercables251
    @supercables251 6 หลายเดือนก่อน

    +1 sub for being the only correct character controller I've ever seen talked about.

  • @ianainoaduarte
    @ianainoaduarte 3 ปีที่แล้ว +3

    Great video, great explanations.
    Saved me a whole lot of headache, but I'm still having problems with the whole applying torque part.

  • @CornRecords972
    @CornRecords972 3 ปีที่แล้ว +3

    This is very cool! Ironic that I found this because I've been building a similar physics based controller but for the opposite. Realistic physics based movement XD
    Best of luck on your game!

  • @milankiele2304
    @milankiele2304 2 ปีที่แล้ว

    I fought so hard to get the best character controller... Your knowledge is the path to fulfillment

  • @thoughtcipher990
    @thoughtcipher990 2 ปีที่แล้ว

    Hey guys, thanks for this video! I just completed our first full playthrough with my family. We all love this game, so thank you so much for your hardwork! 🙏
    In this breakdown, you get goalvel by multiplying the normalized relative input vector by the maxspeed and speedfactor. Can you briefly explain what speedfactor is and why you need it?
    To everyone else reading this, if you haven't purchased this game yet, I encourage you to do so! Let's support game devs who do great work and care about their craft!

    • @Sandiles
      @Sandiles ปีที่แล้ว

      My guess is that speedFactor is just used so they can apply movement effects (slow down and speed up) based on game mechanics. So its not really important to the system as a whole.

  • @pintadenis5641
    @pintadenis5641 2 ปีที่แล้ว

    That Guy Thanks, I will look into it later.

  • @carlosmorais6870
    @carlosmorais6870 2 ปีที่แล้ว

    I'm so confident, yeah, I'm unstoppable today

  • @gonderage
    @gonderage 2 ปีที่แล้ว

    really not gonna lie, im gonna use this method of applying torque to remain upright. It's super fun to mess around with the spring strength and damper values to get wacky reactions! I can get some really wobbly bois that way. Thank you so much for the video!

  • @MaanaDev
    @MaanaDev 3 ปีที่แล้ว +4

    this is very cool! Ironic I'm looking from 2 days
    .
    but u missed active ragdoll to make it more cool and realistic it will be more interesting!

  • @alicem3415
    @alicem3415 2 ปีที่แล้ว

    Thank you. I was having trouble figuring out how to make my Rigidbody controller more responsive. This has made it much better.

  • @OverInfrared
    @OverInfrared 2 ปีที่แล้ว

    Making this work for VR was a bit weird, but worth it. Thank you.

  • @happyavocado7179
    @happyavocado7179 2 ปีที่แล้ว

    5 seconds before you said thats a bit boring I was like dude thats sick

  • @alazuli2291
    @alazuli2291 2 ปีที่แล้ว

    I just tried this method and it really works perfectly for me. Thank you.

  • @sidneiguerrero1
    @sidneiguerrero1 2 ปีที่แล้ว

    What a legend only one ad in the beginning . Your so damn underrated

  • @groovykush
    @groovykush 2 ปีที่แล้ว

    The most structured and detailed tutorial i ca across until now. Thank you very much!

  • @Leonardorth.
    @Leonardorth. 3 ปีที่แล้ว +1

    This is exactly what I've been looking for. Thanks for sharing !

  • @mrpotato8196
    @mrpotato8196 ปีที่แล้ว

    Some things maybe a little over-complicated but considering this is a console game, everything had to be just right, console ppl are pretty strict
    I really liked how you made this (maybe you guys, this game and controller is way too good for one person) I also like making physics based systems since they basically handel everything themselves if you make it right, but everyone either mixes rb based movement with normal movement or just go with normal movement
    Earned a sub!

  • @budhechiranjiv
    @budhechiranjiv 3 ปีที่แล้ว +1

    Nice, Good luck with your release!

  • @Jyotigupta399
    @Jyotigupta399 2 ปีที่แล้ว

    You train so well! It's like you comprehend my tempo...

  • @torryjaja
    @torryjaja 7 หลายเดือนก่อน

    This is absolutely fantastic

  • @Paul-to1nb
    @Paul-to1nb ปีที่แล้ว +2

    Hi! Great video and blog! I was looking at the Character Animation blog post and I'm really confused about the "use an animation curve that defined how to move between single frame poses." I get the idea that you're blending between the poses, but I'm not sure how you can apply the animation curve to a pose.

  • @CosmicComputer
    @CosmicComputer 3 ปีที่แล้ว

    Oh my glob, this is freaking incredible, thank you for sharing this!!!!

  • @tasdude3227
    @tasdude3227 7 หลายเดือนก่อน

    YOOOOOO THANKS MAN EXACTLY WHAT I WANTED!! THANKS!!!

  • @seanisthedude
    @seanisthedude 2 ปีที่แล้ว

    Genuinely a fantastic and informative video. I ended up using some of the methods shown in my own game! I'm absolutely going to buy this game as a thanks (and also because the game looks great :P)

  • @CamoflaugeDinosaue
    @CamoflaugeDinosaue 2 ปีที่แล้ว

    Thanks so much for sharing, this is a very interesting approach! Would reeeeeally love to see your vehicle configuration! It looks like you're using Unity's vehicle/wheel physics, but it's more responsive and snappy than any controller using those Ive ever seen

  • @Usualyman
    @Usualyman 2 ปีที่แล้ว

    This is epic!
    Floating character controller is pretty original decision! Never seen this before)
    Thanx!

  • @mg1632
    @mg1632 2 ปีที่แล้ว

    Very neat take on a CharacterController! Thank you for sharing - subscribed!

  • @rohanvishayal8724
    @rohanvishayal8724 2 ปีที่แล้ว

    Amazing overview and good explanation, thank you looking forward to playing the game.

  • @AimlessGaming1
    @AimlessGaming1 2 ปีที่แล้ว

    Damn bro top tier content, I'm sure you had to visit multiple countries to produce a masterpiece like this

  • @NeonTheCoder
    @NeonTheCoder 2 ปีที่แล้ว +1

    I don't really get the spring math. I wrote exactly what you have for the float mechanic and the capsule either plops to the ground, or starts bouncing infinitely high. What are the values you have set for the rigidbody weight, what are the values for the spring force and damper? they are just off screen and you don't explain how to play around with the values to get this right.

    • @ToyfulGames
      @ToyfulGames  2 ปีที่แล้ว

      looking at the project now, the player's rigid body is average human sized (~2 units tall), and the mass is 10. the Ride Spring Strength is 2000, Ride Spring Damper is 100. Upright (torque) spring strength is also 2000, damper on that one is 30. Ride height is 1, but the raycast length is 1.5 (so 0.5 buffer)

  • @matejzajacik8496
    @matejzajacik8496 2 ปีที่แล้ว

    Wow, this is excellent! Straight to the point and very informative.

  • @sabinudas5395
    @sabinudas5395 2 ปีที่แล้ว

    congratulations fam!!!

  • @tapashsharma6542
    @tapashsharma6542 2 ปีที่แล้ว

    You are doing a wonderful job by giving Knowledge many thanks

  • @Malacord
    @Malacord 2 ปีที่แล้ว

    BROTHER, YOU ARE THE BEST!!! You oooh really helped me!! THANK YOU VERY MUCH!This is cool, well done!

  • @Carlos-dk2lt
    @Carlos-dk2lt 2 ปีที่แล้ว

    bro thanks so much. dis video is tiless 3 years ltr n still great

  • @calarey4250
    @calarey4250 2 ปีที่แล้ว

    It's so simple yet so complicated, it's perfect lmao

  • @CarlosGonzalez-yo6ux
    @CarlosGonzalez-yo6ux 2 ปีที่แล้ว

    Still helping after 2 years

  • @abentertainment786
    @abentertainment786 2 ปีที่แล้ว

    Thank you so much for all these tutorials bro. So much valuable knowledge

  • @mobilelegendlivetest2779
    @mobilelegendlivetest2779 2 ปีที่แล้ว

    I feel you!

  • @hichammarcelcoca8809
    @hichammarcelcoca8809 2 ปีที่แล้ว

    Great Game! I Downloaded It In My Nintendo Switch But In DEMO Version

  • @Loys-vo7cz
    @Loys-vo7cz 2 ปีที่แล้ว

    OMG, it really worked. Thank you so much!!

  • @GhoulyGore
    @GhoulyGore 2 ปีที่แล้ว

    You catch on really fast, it seems complex but once you learn the basics it pretty much branches into experintation

  • @cookiefells_ofc
    @cookiefells_ofc 2 ปีที่แล้ว

    Beautiful man, you're the only one who helped me

  • @diegorg64
    @diegorg64 2 วันที่ผ่านมา

    I don't quite understand how the sticky to slope is implemented. My character, when walking upwards on an inclined surface, sticks too much to the surface, and when it descends, it stays too high, causing the character to make small falls. This is my code that implements the ground hover:
    public void GroundHover(float hoverHeight, float frequency, float dampFactor)
    {
    Vector3 origin = transform.TransformPoint(_capsuleCollider.center);
    bool rayDidHit = Physics.Raycast(origin, Vector3.down, out RaycastHit hit, Data.rayLength);
    if (rayDidHit)
    {
    float mass = _rigidbody.mass;
    Vector3 vel = _rigidbody.velocity;
    Vector3 rayDir = transform.TransformDirection(Vector3.down);

    float springDelta = hit.distance - hoverHeight;
    float springStrength = frequency * frequency * mass;
    float dampStrength = 2 * mass * frequency * dampFactor;

    // spring speed
    Vector3 otherVel = Vector3.zero;
    Rigidbody hitBody = hit.rigidbody;
    if (hitBody)
    {
    otherVel = hitBody.velocity;
    }

    float rayDirVel = Vector3.Dot(rayDir, vel);
    float otherDirVel = Vector3.Dot(rayDir, otherVel);

    float relVel = rayDirVel - otherDirVel;

    // add force
    float tension = springDelta * springStrength;
    float damp = relVel * dampStrength;
    float springForce = tension - damp;

    _rigidbody.AddForce(rayDir * springForce, ForceMode.Force);
    if (hitBody)
    {
    hitBody.AddForceAtPosition(rayDir * -springForce, hit.point, ForceMode.Force);
    }
    }
    }
    My parameter values are:
    - hoverHeight = 1.2f
    - frequency = 15f
    - dampFactor = 0.55f

  • @benmendez7756
    @benmendez7756 2 ปีที่แล้ว

    thank you straight to the point

  • @outdoor1918
    @outdoor1918 2 ปีที่แล้ว

    This was just awesome! Thanks a lot!

  • @UkashaGamers
    @UkashaGamers 2 ปีที่แล้ว

    what a hearty video for all beginners!

  • @jesaroartist1622
    @jesaroartist1622 3 ปีที่แล้ว +4

    This is an amazing video, i just want to ask, what is the first definition for m_GoalVel?

    • @ToyfulGames
      @ToyfulGames  2 ปีที่แล้ว

      it's just the desired movement velocity of the player, coming from the controller input and the movement speed (and translated into world space)

  • @glx6377
    @glx6377 2 ปีที่แล้ว

    It's working thanks my friend

  • @geri4367
    @geri4367 3 ปีที่แล้ว +1

    Awesome, thanks for sharing :D

  • @xvdimmy8220
    @xvdimmy8220 ปีที่แล้ว

    This is gold!

  • @user-dq2ul4bu2v
    @user-dq2ul4bu2v 2 ปีที่แล้ว

    This method works perfectly .. thanks for sharing ;)

  • @fiflax2188
    @fiflax2188 2 ปีที่แล้ว

    Sounds perfect!!!

  • @LeSandWhich
    @LeSandWhich ปีที่แล้ว

    the force when the character reach the top reduce to 0, then the gravity will push it down again, making the character just bobbing up and down. what did I miss ? turn gravity off wont do, cause the character then will just float around and other funky sh1 t.

  • @margaritamoreno2076
    @margaritamoreno2076 2 ปีที่แล้ว

    Tysm, did everything as described

  • @spikebor
    @spikebor 2 ปีที่แล้ว

    a very great system and carefully explained, thanks !

  • @ajajjssjdjdndns1917
    @ajajjssjdjdndns1917 2 ปีที่แล้ว

    Oh my god so good explained thank you!!!!

  • @AlexBlackfrost
    @AlexBlackfrost 3 ปีที่แล้ว

    Wow, great video! Thanks for sharing!

  • @ruchirraj5300
    @ruchirraj5300 2 ปีที่แล้ว

    Can you elaborate a bit more about how you calculate jumping force?
    It would help me a lot. Thanks

    • @ToyfulGames
      @ToyfulGames  2 ปีที่แล้ว

      For jumping we just set the velocity directly when the jump starts, no need for a force at all.

  • @vishalsenapati3879
    @vishalsenapati3879 2 ปีที่แล้ว

    that was so use full! !

  • @rajatgoswami7072
    @rajatgoswami7072 2 ปีที่แล้ว

    damn the quality of video editing

  • @nthnnnnnnnnn
    @nthnnnnnnnnn 3 ปีที่แล้ว

    Looks really cute

  • @denji6763
    @denji6763 2 ปีที่แล้ว

    you explained. Thank you so much.

  • @selfmades4494
    @selfmades4494 2 ปีที่แล้ว

    So informative, thanks a lot!

  • @Ziboo30
    @Ziboo30 3 ปีที่แล้ว

    This is gold ! thanks for sharing

  • @shingAMarie
    @shingAMarie 7 หลายเดือนก่อน

    do you, or anybody else have tutorials explaining this for sort of beginners. I still don’t understand the basic code of sending a line trace from the character to the ground to see how far they are from the ground. I need to understand that concept and how it goes into Code. Can you please help with that or refer me

  • @ElSonk
    @ElSonk 3 ปีที่แล้ว

    Super excellent! Thanks a million

  • @hebashamaa7695
    @hebashamaa7695 2 ปีที่แล้ว

    Very helpful, thank you

  • @Fghjkjhljhghj
    @Fghjkjhljhghj 2 ปีที่แล้ว

    This was a great tutorial, thanks a lot!

  • @apukumarsarkar7016
    @apukumarsarkar7016 2 ปีที่แล้ว

    Thanks for the tutorial

  • @Arcal-sx6ls
    @Arcal-sx6ls 2 ปีที่แล้ว

    Thank you In the setup

  • @petrussian8228
    @petrussian8228 2 ปีที่แล้ว

    Thank you so much it was very helpful

  • @ian_snyder
    @ian_snyder 3 ปีที่แล้ว

    This is amazing, sharing with my students :)

  • @camilatontis
    @camilatontis 2 ปีที่แล้ว

    Cheers man!

  • @TedThomasTT
    @TedThomasTT 3 ปีที่แล้ว

    You guys are saints for sharing this knowledge