Hello, I have been away for a while because of life but I’m back to finish this course. I am on lesson 97 (as per Udemy) and I have a weird quirk that I can’t figure out. I never put an enemy in the level before because I figured we’d get to it, but I decided to put on in after the decorations/“improvements” lessons to make sure everything is working.
It kind of is, but kind of isn’t.
So for some reason the enemy is floating above the ground about half a dozen pixels. I made sure the Y-axis is 0, but for some reason no matter what I do it is always floating. Also, when the player gets to the detection sphere, the enemy zooms over to the player instead of walking normally. I compared my C++ Enemy file to the instructor’s, and everything seems to match. I’m wondering if this is an issue with how I have things set up in Unreal? I see settings for Max movement speed, max acceleration, max custom movement speed, and others but nothing about actual movement speed. Also nothing to indicate that the enemy should be hovering.
Anyone have any ideas on what I could have done wrong?
EDIT:: Went back and realized that the instructor set the maximum walk speed to 100. did that as well and it fixed the zoomies. Is max walk speed the variable to decide how fast the enemy moves? Why wouldn’t there be a walk speed variable, then max and min speed variables as well? like if we want an enemy to run sometimes but walk other times. Still not sure why the crab is floating.
EDIT 2:: And I figured it out. Went back and watched the enemy creation video, and I had set the capsule height to 34 instead of 24. That would most likely be why it wasn’t touching the ground, as the capsule was extending past the sprite. Still curious about the walk speed settings from the first Edit.
Ah I was going to say Robert, the capsule height.
The movement Max Walking speed is the really that. It’s meant to handle AI which always goes as fast as you allow it but also player input, where the gamepad for example is analogue so you get the value from -1 to 1 and the Max walking speed is multiplied by this value so a small stick movement would be 0.2 x the speed. This is why it is called Max rather than just walking speed.
For keys, it is always 0 or 1 really so it just goes as fast as it can, a bit like AI Characters.
I hope this makes sense.
ah, I understand. so it is a binary option by default.
in that case, how would you implement variable enemy speed? I’d assume you’d have to do it using C++, like if player is within the 1st sphere, run. if player gets to the 2nd (inner) sphere, walk. I don’t really see an option to implement something like that in the settings we have. would it be a custom function, set up like movement in the Raylib C++ course I took here?
You can use a collision sphere and on overlap, update the max walk speed to a faster speed. You’d probably need 3 collision spheres. One to set back when the player exceeds a distance. you could probably do this with a tick to be honest. So my expertly drawn diagram shows the idea. Enter green is fine, enter blue causes speed-up. Enter red to attack.
That is one way.
The other is when the player enters the green circle, the enemy gets the player and uses a simple distance calculation on the location of the 2 actors. Escape distance exceeded, clear the player reference and set the speed back to normal. This has the added advantage of working in any direction so up as well.
Thing is, behaviour trees can do a lot of this for you.
Think about this anyway and see what fits. Th collision sphere approach is probably more costly as it will always have the 3 spheres. I think at least you’d need the two - the enemy hit capsule and the collision sphere for both solutions. Both will work. In both cases you need to check see which enemy is passing into the detection sphere.
1 Like
interesting, but can you access the Unreal variables from Visual Studio? like, to change max speed in code? You could change to a running animation using an interrupt on overlap, but I thought variables like Max Speed would be walled off from VS?
Yes. Just get a reference to the movement component. I think it is simply GetMovementComponent()
1 Like
interesting. I really wish the C++ documentation was better. It would be much easier if there was a resource page like there is for standard C or C++. I guess technically there is an online database but it seems to be just for UE4 and it is a mess to navigate and it doesn’t explain or give examples.
so in a 2d game, would having multiple spheres be that much more taxing? would it make more sense then to create a “box” around the player with simple math like we did in the Raylib C++ course? then we could just change the max movement speed like that instead of some overlap function with a ton of overhead. do it in code with one function. seems deceptively simple.
No, this is fairly simple so it wouldn’t put too much pressure. Spheres for detection are better as you can specify a distance which then applies in all directions. There are inbuilt functions for this in the behaviour trees but it is not difficult to do. You can also do it by having the enemy calculate the distance between 2 locations (player and self) as well and when it exceed the distance, you clear the player and the enemy goes back to wandering around.
Does this make sense?
I understand. I figured the “old school” way would be less intensive since it was just relying on basic math, but if the spheres aren’t too taxing that works as well.
1 Like