• Godot HelpProgramming
  • How can I alternate between animations during user Input with a Animated Sprite3D node?[ANSWERED]

Hi @TwistedTwigleg,

Well I tried the code and sadly enough it doesn't work.

I have been poking around in the code seeing what works and what doesn't with no result. What I mean is that : Yes the character does jump and the Gravity (really slowly) drags him back down but the camera just clips to the new position of the character.

And that is / was my question back then but I frased it wrong, I really wanted to know how I could make a jump function work and have the camera move with it without the jump the camera gets.

This can eventually become irratating for players so i want to prevent it in it's early stages =)

Thanks

What I mean is that : Yes the character does jump and the Gravity (really slowly) drags him back down but the camera just clips to the new position of the character.

The numbers probably need to be adjusted. Also, you may want to add incremental gravity, gravity that gets stronger as the player is in the air, to improve how jumping "feels", if that makes sense.

The code is written hastily, and without any comments, but on my Dreams of Fire GitHub repository there is code for a 3D character that uses incremental gravity, which may be helpful if you are looking to implement incremental gravity.

And that is / was my question back then but I frased it wrong, I really wanted to know how I could make a jump function work and have the camera move with it without the jump the camera gets. This can eventually become irratating for players so i want to prevent it in it's early stages =)

To be honest, I'm a little confused. You want the camera to jump with the player, or do you want the camera to stay in place on the Y axis while the player jumps? Also, is the camera attached to the player scene, or is it separate?

@TwistedTwigleg said:

To be honest, I'm a little confused. You want the camera to jump with the player, or do you want the camera to stay in place on the Y axis while the player jumps? Also, is the camera attached to the player scene, or is it separate?

The camera is part of the character, so in the hierarchy: Player -> Spatial (on the character) -> -> Camera

Well what I mean is that now that the character jumps into the air, it looks like the Camera is snapped to a new position. This could be the speed at which the character travels (or something) but the Camera needs to follow the player but smoothly and not with jidders or shocking. Just a normal and smooth follow, like Mario games or any other platformer.

Ah, I think I see now!

Because the camera node is attached to the player, it is moving at the same speed that the player moves. However, the speed of the player causes the camera to jump from place to place, which is disorienting and not very smooth.

Unfortunately, I think the easiest way to fix this is probably to separate the Spatial and Camera node from the player, and then write a script to make the Camera follow the player smoothly using a transform lerp. I have not tried it myself, but off the top of my head, this is how I would write the code to make the camera follow the player:

extends Spatial
export (NodePath) var path_to_target = null
var _target = null
export (float, 0, 1) var lerp_speed = 0.9;

func _ready():
	if (path_to_target != null):
		_target = get_node(path_to_target)
		set_process(true)
	else:
		print ("Target not set for Camera with name :", name)
		set_process(false)

func _process(_delta):
	var target_transform = _target.global_transform
	global_transform = global_transform.interpolate_with(target_transform, lerp_speed)

I think the code will then follow the player smoothly. You might want to multiply lerp_speed by delta to make it frame rate independent, but then you'll probably need to add code to clamp it in the 0 to 1 range so the lerp acts as expected when delta is very high or low.

@TwistedTwigleg I think my code isn't correctly set because my camera doesn't have a target eventhough I (think I) have set it to the name of the player.

I also found out that it is my character that is the one teleporting to the new spot instead of the camera.

But that changes my question to: How can I make a smoother jump?

Thanks for your help, btw =)

https://drive.google.com/file/d/1lqn0Rsi4QxFgd5tEnUjv8mzERK68jrLn/view?usp=sharing![] A gif of my character using it's jump function.

(https://i.imgur.com/5t3onig.png)

@KindosaurGaming said: @TwistedTwigleg I think my code isn't correctly set because my camera doesn't have a target eventhough I (think I) have set it to the name of the player.

To set the target, you'll have to set the NodePath. Because it is an exported variable, you can set the target by selecting the node in the Godot Editor, and in the inspector there should be a variable called path to target that you can set.

I also found out that it is my character that is the one teleporting to the new spot instead of the camera.

But that changes my question to: How can I make a smoother jump?

Well, that would explain the teleporting! What does the code for jumping look like?

For smooth jumping, I'm not sure what the best way is to achieve it. Each method I have tried has its own pros and cons. The easiest method is probably just to set the velocity on the Y axis to a positive value, but this can cause issues at high values and doesn't account for longer jumps when the button is held down.

The KinematicCharacter demo implements this type of jumping. I'd start with implementing something similar to that, unless you are already using something similar, and see if that smooths out the jump.

Thanks for your help, btw =)

Happy to help :)

@TwistedTwigleg I have implemented the code to my 2D section of the game with no problem and I have no set it into my 3D section but now I can't turn my camera on its X axis (horinzontally).

These are the lines I used for the jump function

JUMP_SPEED is high but it gives a low jump.

AnimatedSprite3D const JUMP_SPEED = 200.0 func _process(delta): if (Input.is_action_just_pressed("Jump")): velocity.y += 100 velocity.y += applied_gravity * delta

@KindosaurGaming said: @TwistedTwigleg I have implemented the code to my 2D section of the game with no problem and I have no set it into my 3D section but now I can't turn my camera on its X axis (horinzontally).

Thinking about it, it is probably because of the transform lerping. Since the entire transform is lerped, rotations cannot be applied. This can be relatively easily fixed by lerping the origin/position instead of the transform. Try replacing _process in the camera script with the following:

func _process(delta):
	var target_origin = _target.global_transform.origin
	global_transform.origin = global_transform.origin.linear_interpolate(target_origin, lerp_speed)

That should fix the rotation issue.

Since the jumping seems to be causing the issue, you can probably just have the camera as a child of the Player again if you want. Once the jumping issue is resolved, it should work like before but without the teleporting.

These are the lines I used for the jump function

JUMP_SPEED is high but it gives a low jump.

AnimatedSprite3D const JUMP_SPEED = 200.0 func _process(delta): if (Input.is_action_just_pressed("Jump")): velocity.y += 100 velocity.y += applied_gravity * delta

Maybe try replacing velocity.y += 100 to velocity.y = JUMP_SPEED and see if that helps. You'll probably need to adjust JUMP_SPEED though, as the jump will no longer need to counteract gravity when the key is pressed (since the velocity is being set rather than added to).

@TwistedTwigleg
YES! The camera is following the player correctly and the camera can be moved like it should. The jump is a bit better, I can see a sped up version of my character rising to its maximum height.

What I mean with that is that it looks like the JUMP_SPEED function sets some sort of roof on him and thereby prevents the character to jump higher than the set JUMP_SPEED.

So the character hits his head on a invisible roof with the force applied by the JUMP SPEED.

Might want to look into the tween node and the different interpolation curves that can be used...

@Megalomaniak I haven't used a Tween Node in my project but can it be applied to a 3D world.

Also. If you make a character jump In 3d you will always get a smooth jump not a teleport jump so I might need to check in on the velocity.y part of my code.

@Megalomaniak I am going to look into this, looks like a helpful tool indeed :P

Thanks

@Megalomaniak @TwistedTwigleg Sorry but I myself was not able to understand the ways the tutorials showed the code to me for the Tween Node. I mean like it has something to do with the change in positioning and movement of a node. but for some reason I wasn't really able to replacate this into my project. Or i might be overlooking some of the things presented to me.

I am no longer in the beginners phase and I am now able to understand what it says and I might sometimes be able to make my own lines of code work or even remake them to work. (I really begin to love coding more and more)

But I did some digging through the (amazing) code given by @TwistedTwigleg and I found the problem of my jumping being all weird. The problem lays inside of the Velocity code. You used 2 versions of it: Velocity.ZERO and `Velocity = Vector3(0, -1, 0). By removing the latter and replacing the first I was able to make my character do a smooth jump. But now the character shoots forward (which I was able to recode to make him stop doing that) and he can't walk in 8 directions (Animations do still work when keys are pressed), lastly he doesn't stop when the key isn't pressed anymore.

So there needs to be a way to still have the 8 sided movement pattern and disable the weird Velocity during a jump.

I am not sure if I am correct on this one. but again I am glad that you are willing to help me out with this one :P

I'm glad you were able to figure out how to make the jump smooth!

The movement is, probably, because the velocity modifications made by pressing the arrow keys are either no longer modifying velocity, or their modifications are being lost later in the code. I'd suggest looking through the code and seeing if you can deduce where velocity is being changed on the X and Z axes that is not movement from player input. That might help find the issue.

9 days later

@TwistedTwigleg I have been trying for a few days now to see if I was able to see the line of code that detects the Z and X velocity but with no succes really.

The only things that I found was that: - Vector3.ZERO is a different way of saying Vector3(0,0,0)

  • By repeating my steps again (and this time make my move_speed slower) I saw that this bug was now only affecting my diagonal jumping meaning that there is indeed a line of code (probably the Velocity) creating this bug

  • By removing that part I also saw that my character is infinitly accelerating (meaning that I need to add a const MAX_SPEED) and the character doesn't stop moving in that direction and accelerates when pressing to a diverent direction

  • Apparently the gravity is hereby effected to (don't know why)

  • When I fall of a platform it will drop me instantly so no acceleration just an instant drop just because I tinkered with the GRAVITY a bit

  • The camera isn't (luckily) the part of any bugs / glitches when jumping around nor is does it take part in the extreme acceleration of the character

During the writing of this I am now able to JUMP in all 8 directions normally. But with weird properties seen above.

SOo.. We are getting close to the solution :P The bugs (seen above) might be simple to fix I hope

@KindosaurGaming said: @TwistedTwigleg I have been trying for a few days now to see if I was able to see the line of code that detects the Z and X velocity but with no succes really.

Do you mind sharing the code? I do not know if I'll spot anything, but I can try. Or a sample project would be even better, as that way I could try modifications and run the project to see the results.

Unfortunately, my schedule is really busy right now, so my ability to help is probably going to be very limited in the near future. I'm hoping things will clear up a bit soon though.

The only things that I found was that: - Vector3.ZERO is a different way of saying Vector3(0,0,0)

Yup!

  • By repeating my steps again (and this time make my move_speed slower) I saw that this bug was now only affecting my diagonal jumping meaning that there is indeed a line of code (probably the Velocity) creating this bug

Maybe the code for figuring out the direction the player is moving in has a y component to it, and so it is modifying the strength of gravity based on the camera angle? I don't know if that is the case, but it might be something to look at.

Maybe try canceling the Y value in view_camera_forward and view_camera_right after they are defined/set:

var view_camera_right = view_camera.global_transform.basis.x
var view_camera_forward = view_camera.global_transform.basis.z
view_camera_forward.y = 0
view_camera_right.y = 0

I'm not sure if that is causing the issue, but it might be something to try.

  • By removing that part I also saw that my character is infinitly accelerating (meaning that I need to add a const MAX_SPEED) and the character doesn't stop moving in that direction and accelerates when pressing to a diverent direction

Hmm, strange. I'm not sure what would be causing this issue right off.

  • Apparently the gravity is hereby effected to (don't know why)

Gravity is affected by diagonal movement? If so, that might suggest that the issue I mentioned above, that the view camera is modifying gravity, is the issue. It is not a guarantee, but it definitely seems possible.

  • When I fall of a platform it will drop me instantly so no acceleration just an instant drop just because I tinkered with the GRAVITY a bit

I'm guessing, but this is probably due to the increased gravity. You may need to disable increase gravity or reset it when the character is standing on a platform.

  • The camera isn't (luckily) the part of any bugs / glitches when jumping around nor is does it take part in the extreme acceleration of the character

Great! That means whatever the issue is, it is not there. That should make it a little easier to narrow down what is causing the issue.

During the writing of this I am now able to JUMP in all 8 directions normally. But with weird properties seen above.

SOo.. We are getting close to the solution :P The bugs (seen above) might be simple to fix I hope

I think they should be easy bugs to fix, it just might be somewhat difficult to find what is causing the bugs. With enough debugging and tinkering, I'm sure the bugs are fixable! :smile:

@TwistedTwigleg I will let you know if I fixed everything and I will also provide a picture of the code

@TwistedTwigleg In the link provided below you can see for yourself what I did it has the player inside with his animations (you probably won't need it) and also my script + test environment

Ok I have tinkered some more and now the following things happen: - The character is now only able to walk in 4 directions, but if you point the camera on the X and Z axes he can walk everywhere - Character still accelerates btw - If you switch from direction (from left to right for example) the character sometimes slides a while before walking in its new direction - The character can infinitly jump

But things that work properly now are as follows: - The character does stop when no longer pressing a button (but with quick switching it is a bit odd) - Jumping works properly, a bit floaty but it is a lot better and can be dealt with with the proper gravity settings - Jumping doesn't get lower and lower after each input - When Using the diagonal movement everything seem to be fine no matter the point of view of the camera

Hopefully I was able to sent the right things so that, when you got some rest ;), maybe could take a look into my code. Because I am now only stuck in the X and Z axis issue really.

https://drive.google.com/file/d/1ghaRueBbMuZviStqVW0p8LiGlBJbGCVs/view?usp=sharing

Okay, I had a spare moment and so I downloaded and tinkered with the project. I was not able to get the animations working, something with the import process went wrong, so I just used one of the sprites so I could see what I was doing.

The biggest issue was how the input was being managed. Directly modifying velocity turned out to be a bad idea, so I took the same type of code that I used in the FPS tutorial and made adjustments so it works.
Now the code no longer directly modifies velocity, and instead uses a temporary vector, and then applies the direction after all input. This fixed the movement/input issues, including the directional input.

The FPS code changes/addition also makes it where the character has acceleration and deceleration. This fixes the issue where the character accelerates to extremely high speeds, and also fixes the sliding issues.

I fixed the infinite jumping by checking if the character is grounded using a raycast. I was going to use the KinematicBody node, but the is_on_floor function was acting inconsistently and so I decided to use a raycast. Using a raycast fixed the infinite jumping issue.

I also changed how gravity works a bit, so the additional gravity was accumulative rather than static. This makes the jumping a little more snappy, though I had to tweak the values a bit. I also added some code so if the player walks of the edge, they are not snapped to the ground due to the strength of gravity.

I think I also made some other minor adjustments. I added comments showing what I changed, removed (or rather commented out), or added. I uploaded the project on my Google drive. I think all of the issues mentioned are now fixed.