Announcement

Collapse
No announcement yet.

Change Tire on Runtime (BP/with a tiny bit of C++)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Change Tire on Runtime (BP/with a tiny bit of C++)

    Updated Tutorial ;
    https://forums.unrealengine.com/deve...ing-tires-c-bp

    Hey everyone,
    So today i figured out how to change wheels on runtime. So you dont have to go trough the same annoyance i went trough heres how to set it up.

    Instructions:
    1 : click file -> new C++ Class -> BlueprintFunctionLibrary (in my case i named it TireLib)
    2 : After that head over to your (Projectname).build.cs inside visual studio (Game/Projectname/Source/Name/Projectname.Build.cs)
    3 : Make sure "PhysXVehicles" is in the dependencys (seen in screenshot)
    4 : Populate the .hFile of your library (step 1)
    Code:
    #include "WheeledVehicleMovementComponent.h"
    Code:
    USTRUCT(BlueprintType)
    struct FWheelSetupBP
    {
        GENERATED_BODY()
    
            // The wheel class to use
            UPROPERTY(BlueprintReadWrite, Category = WheelSetup)
            TSubclassOf<UVehicleWheel> WheelClass;
    
        // Bone name on mesh to create wheel at
        UPROPERTY(BlueprintReadWrite, Category = WheelSetup)
            FName BoneName;
    
        // Additional offset to give the wheels for this axle.
        UPROPERTY(BlueprintReadWrite, Category = WheelSetup)
            FVector AdditionalOffset;
    
        // Disables steering regardless of the wheel data
        UPROPERTY(BlueprintReadWrite, Category = WheelSetup)
            bool bDisableSteering;
    };

    Code:
    UCLASS()
    class TESTCPP_API UTireLib : public UBlueprintFunctionLibrary
    {
        GENERATED_BODY()
    
        UFUNCTION(BlueprintCallable)
        static void modifyTyreConfig(UWheeledVehicleMovementComponent* Component, TArray<FWheelSetupBP> Tyres);
    };
    Make sure to replace tirelib with your filename

    5: lastly go into the c++ part of the libary we created and include

    Code:
    void UTireLib::modifyTyreConfig(UWheeledVehicleMovementComponent * Component, TArray<FWheelSetupBP> Tyres)
    {
        TArray<FWheelSetup> CTyres;
        for (int i = 0; i < Tyres.Num(); i++)
        {
            FWheelSetup tire;
            tire.WheelClass = Tyres[i].WheelClass;
            tire.BoneName = Tyres[i].BoneName;
            tire.AdditionalOffset = Tyres[i].AdditionalOffset;
            tire.bDisableSteering = Tyres[i].bDisableSteering;
            CTyres.Add(tire);
        }
        Component->WheelSetups = CTyres;
        Component->CreateVehicle();
        Component->RecreatePhysicsState();
    }



    After that you just have to compile and you will be able to setup your new tire seen in the Bp example.

    Thank you Moddinggear over on Discord for helping me with the C++ and the setup.

    If you have questions or issues please let me know below.

    Last edited by dollon; 04-06-2019, 10:32 AM.

    #2
    Very good! this works very well. I was having to change the vehicle wheelclass variables but the problem is that when I change a variable, all other vehicles that use the same wheel will change too, so I was trying to create a wheelclass to be an instance for each vehicle when it was created just like any other normal c ++ variable but I was not successful, I believe I have to create a copy of the wheel to use on each created vehicle because it uses a TSubclassOf <UVehicleWheel> but all my attempts have failed. Do you know how to do this?

    Comment


      #3
      Originally posted by BiancaMello View Post
      Very good! this works very well. I was having to change the vehicle wheelclass variables but the problem is that when I change a variable, all other vehicles that use the same wheel will change too, so I was trying to create a wheelclass to be an instance for each vehicle when it was created just like any other normal c ++ variable but I was not successful, I believe I have to create a copy of the wheel to use on each created vehicle because it uses a TSubclassOf <UVehicleWheel> but all my attempts have failed. Do you know how to do this?
      Sadly not directly Doesnt it work to just run it on a local client ? It should only change the config of the active vehicle. If you still have those issues let me know.

      Comment


        #4
        When i work in the file i'm not able to compile... Do you know from where the problem can coming?
        edit: i'm going to reinstalle Visual Studio...
        edit: That was cause by a directory name...
        Last edited by Hêval; 09-27-2018, 10:40 AM.

        Comment


          #5
          How we can have "PhysXVehicles" is in the dependencys when you don't have?

          Comment


            #6
            Originally posted by Hêval View Post
            How we can have "PhysXVehicles" is in the dependencys when you don't have?
            You just type it in! :-)

            Just wanted to say thank you dollon , this opened up a world of opportunities for me. Cheers! :-)
            Last edited by Korizon; 01-18-2019, 07:01 PM.

            Comment


              #7
              I have this working for the most part, it does indeed update the selected wheel class and the physics (my vehicle will drop down or raise up accordingly).
              However, I can't get the vehicle to move any more. Below are some notes I've made to try and sort this out:

              I can see the front wheels trying to steer but they can't rotate past about 1.5 degrees
              Throttle, brakes, handbrake all register fine as input but there is no response
              The suspension seems to be going wonky and is constantly being upset by .01 or -.01
              The only gears available are Reverse or 1st, Neutral doesn't exist
              I noticed that setting a vehicles default wheels to None (cleared) behaves similarly

              Can anyone give some insight to something I am doing wrong, or that may need fixed in the code?

              Thanks ahead of time!

              Comment


                #8
                Originally posted by Korizon View Post
                I have this working for the most part, it does indeed update the selected wheel class and the physics (my vehicle will drop down or raise up accordingly).
                However, I can't get the vehicle to move any more. Below are some notes I've made to try and sort this out:

                I can see the front wheels trying to steer but they can't rotate past about 1.5 degrees
                Throttle, brakes, handbrake all register fine as input but there is no response
                The suspension seems to be going wonky and is constantly being upset by .01 or -.01
                The only gears available are Reverse or 1st, Neutral doesn't exist
                I noticed that setting a vehicles default wheels to None (cleared) behaves similarly

                Can anyone give some insight to something I am doing wrong, or that may need fixed in the code?

                Thanks ahead of time!
                Hmmm~
                maybe your new wheel config has too high values for grip or something. Because if you got it working to the point of swithing the wheels it should work no issue. What version are u using ?

                Comment


                  #9
                  Has anyone gotten this working on UE 4.21? And how? Please!

                  Comment


                    #10
                    Originally posted by porckchop View Post
                    Has anyone gotten this working on UE 4.21? And how? Please!
                    I have an idea how to fix it would need to check a few things out first tho.I'm
                    Currently, in between building a company and my day job so time is a bit rare but ill try my best.

                    Comment


                      #11
                      You area awesome dollon

                      Comment


                        #12
                        Originally posted by porckchop View Post
                        You area awesome dollon
                        Got time tiday finally sorry man for this takeing so long but i reccomend just keeping an eye out on my youtube gonna figure it out today and make a video tutorial after. Im still sorry for this taking so long but work just was a bit too much.
                        https://www.youtube.com/channel/UCfW..._as=subscriber
                        But i will post again as soon as i fixed it.

                        Comment


                          #13
                          Rewrote the code No compiling issue. Was a pain to find the Updated Classes but it worked.

                          Comment


                            #14
                            https://forums.unrealengine.com/deve...ing-tires-c-bp

                            Comment


                              #15
                              Hey dollon, have you ever found out how to remove sticky wheels, i think thats what causes the car to stop dead in it's tracks when impacted. We cant seem to get it.

                              Comment

                              Working...
                              X