Hey guys I’m new to unreal 4 and just going to say, it’s really awesome and cool! ^^ So I have this college assignment to make a playable 3D game with some interaction, so I decided to do a time attack racing game, everything is going great; using the car blueprint project and managed to get car AI working thanks to a tutorial online. But I have one little thing I want to add in my game but I can’t find any tutorials so I decided to post this to you guys to see if you could help me out with this. :3
I basically want to make it so if my car crashes and falls on it’s side/upside-down I could simply press a binded key to make it respawn at the same place it tipped over.
Okay new problem, I followed the guide and it doesn’t work for me sadly, when I start the game and press R it just destroys my car and nothing happens after that… Here’s a few screenshots
When you cast to the sedan in the GameMode, the object reference should be the character, not the controller. Basically, you are binding the OnDestroyed event to the Sedan, but are telling the controller to respawn, not the Sedan itself. Try changing that, and let me know if that fixes it.
LOL sorry about that, I just posted the example from the documentation. What I was trying to hint at was in your Game Mode (i.e. MyGame), from the “Cast To Sedan” node, change the “Object” pin connection from “Get Player Controller” to “Get Player Character”. The easiest way to do this, is to just select and delete the “Get Player Controller” node, then drag off the “Object” pin and type “Get Player Character”. I’m not at home, and don’t have access to UE4 right now, but can upload a more accurate image if necessary.
So… this gets a little tricksy. Since you are dealing with the Sedan BP as your Player Pawn (not a character based class, but pawn based), you need to cast to it’s parent first to be able to talk to it directly. If you look up to the top-right corner of your Sedan BP, you will notice it says “Parent class: Wheeled Vehicle”. This means that it not only is the Sedan, but it is derived from the Wheeled Vehicle class as well (which gives it physics and such). In order to go from the Game Mode to the Sedan, we need to get the pawn, cast it to the Wheeled Vehicle class, then recast to the Sedan.
Also, something fun to play around with during casting, is the “Cast Failed” pin. This lets us do something else if the cast ever fails on us. I added a “Print String” node, and typed in some information to see how far the casting was actually getting for quick & dirty debugging (there are much more elegant ways to debug, I’m just not there yet LOL).
The only issue I’ve run into so far, is that whenever I make it respawn the car, it will only do it completely one time. I’m not 100% sure why this is the case, but don’t have much time to really look into it now, sorry. If you need further help though, I am willing to help and learn when/where I can.
Aha! It works thank you ^^ And mine keeps repeating so that’s a plus
So I have another question related to this, instead of respawning to the PlayerStart location is it possible to make something that whenever I hit respawn, it will take me to the closest place to respawn on the track instead of taking me all the way back to the start of the track, because as you see I’m making a time attack-ish game and my sedan tends to tip over abit if you’re not careful.
In short, what we’ve done is told the Sedan BP that when we press the F key, we are going to tell the Game Mode to set the Spawn Transform (where we get spawned) to the Actor’s current Transform prior to it being destroyed. Then we destroy the actor, and the Game Mode respawns the pawn in the new set Transform.
Something to consider: if you are going to have your actor flip over/run off-road/sink in a lake/etc., you probably won’t want to use this method specifically. However, this is a very basic idea, and should get you moving in the right direction. Use this as your base, and figure out what it is you need to accomplish to make the actor spawn somewhere other than it’s current location (location of something else), as well as to set it’s orientation on spawn (upright, facing the correct direction, etc.).
Okay it works, but now I want to know how to change it’s position to make the vehicle stand up again like you said being able to face the right direction on the track when it spawns.
LOL that it is! I’m in the same exact boat as you are when it comes to this kind of stuff. There doesn’t seem to be enough real information out there for us beginners to transition from beginner to intermediate (not that Unreal Engine 4 is really that old, or I am any good at it! LOL). Anyway, here’s my suggestion:
In the Unreal launcher, select the Learn tab, then scroll down to the Example Game Projects section. There, you will find a Vehicle Game project (I just found it myself a few days ago). Download it and check it out. Inside of the VH_Buggy Blueprint, there is a section sequenced off of the Event Tick that should help with determining if the vehicle is stuck (instead of forcing the user to press a button to respawn, but I’d keep the button for the development cycle so you can at least test the functionality easier).
Most of the real meat of the project is done in C++, but what it looks like to me is that they have created CheckPoint actors with an arrow facing the correct direction on the track. Whenever the buggy overlaps the actor, it records that CheckPoint as the last one it touched. If the buggy gets stuck, it is destroyed and respawns at the last known CheckPoint actor, facing the same direction as that actor. You can accomplish all of this in Blueprints. What I don’t know for certain is whether you will have to cast to the level Blueprint from the GameMode to get that CheckPoint reference, or if you’ll need to handle respawning in the level Blueprint instead.