Timing an animation to end before character hits the ground
Asked Answered
T

9

0

I want to make an animation like the one showed in the video

anim-demo.mp4
3MB

I want the animation to speed up or down with velocity, such that it ends just before the landing.. however when jumping up, the player is not able to complete the animation. The only way by which I think this can be done is by using the formula time = distance/speed and change speed_scale accordingly. Nut I have been unable to calculate it since velocity is always changing
Any ideas how to go about this? Here is my code

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY= -500.0
var anim : AnimationPlayer

@export var gravity : float = ProjectSettings.get_setting("physics/2d/default_gravity")


func _ready():
	anim = $AnimationPlayer

func _physics_process(delta):
	_calculate_movement(delta)
	move_and_slide()


func _calculate_movement(delta:float):
	if not is_on_floor():
		#Change speed_scale with velocity
		anim.speed_scale = abs(velocity.y/100)
		velocity.y += gravity * delta
		if Input.is_action_just_released("jump") and velocity.y<0.0:
			velocity.y += 100
			
	elif Input.is_action_just_pressed("jump"):
		anim.play("Rotate")
		velocity.y = JUMP_VELOCITY
	
	var direction = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	if direction != 0:
		velocity.x = SPEED * direction
	
	~~else:
		velocity.x = lerp(velocity.x,0.0,0.1)
Tudor answered 3/5, 2023 at 7:4 Comment(0)
L
0

So you want the finish the animation exactly when the player touch the ground? Thing is you have to know how long the player will be in the air but i don't know if it's possible to know that precisely since the player control the box

Laurielaurier answered 3/5, 2023 at 10:53 Comment(0)
T
0

Laurielaurier Basically yes. I want the animation to end exactly like it did in the video. I could kind of do it by just measuring distance and speeding it up but its still not foolproof and fails several times. even if I could somehow do it with contunious raycasts, it would be way too expensive for such a simple effect. There's got to be an easier/more optimized way

Tudor answered 3/5, 2023 at 11:56 Comment(0)
L
0

You don't want to use an AnimationPlayer for this.
But I think you will have to rotate only the visual instance, as for now you're also rotating the physical instance and that could lead to troubles because it means just before it touches the ground it should be rotating fast because your box will have a "high" velocity at this instant, and you box corners will contact.

So in fact you can just do something like:
rotation += velocity.y
and when the body is on floor you set it back to the initial value.
Otherwise i personally don't see how you could solve this sorry

Laurielaurier answered 3/5, 2023 at 12:44 Comment(0)
P
0

Using physics prediction is the best way, and not too expensive

Pervade answered 3/5, 2023 at 15:29 Comment(0)
A
0

You could try looking using a RayCast2D to measure the distance between the player and the ground, and lerp the SPRITE'S rotation degrees depending on that distance.

Accra answered 4/5, 2023 at 2:12 Comment(0)
T
0

Accra Tried that already. The issue with that is that the y velocity is almost never constant, and I also need to y velocity to accurately get the time before I hit the ground.

Tudor answered 6/5, 2023 at 7:49 Comment(0)
T
0

Pervade If possible, can you please elaborate? I'm relatively new to making games in general, so I dont know how to "predict physics" if thats what its called. I did some searching but only got results for rigidbody, none fore kinematic body. Im fine with switching to rigidbody if that works well enough and doesnt mess with the physics too much in terms of movement

Tudor answered 6/5, 2023 at 7:53 Comment(0)
P
0

Tudor

Try a tutorial like this:

But if you are new to making games, you may not want to get this advanced right away. Really depends on how important this is to your game. I'd recommend just understanding the basics and then fine-tuning later . Fine-tuning usually involves a lot more work than the core features!

Pervade answered 6/5, 2023 at 13:25 Comment(0)
T
0

Pervade After some more exploration, yeah it is pretty advanced for me yet and its not really that important. I'll see what to do with it later.
I had another problem. I don't know whether I should create a different post for this, so I'll just ask this here
I want to NOT play the animation at all of the player is touching a wall when the animation is placed. But

if not is_on_wall() and not anim.is_playing("Rotate"):
         anim.play("Rotate")

Does not work and is_on_wall returns falls even when touching a wall. Should I just use raycasts or am I using the is_on_wall() wrong?
P.S. Sorry for replying late.The internet at my place was down for a couple of days

Tudor answered 9/5, 2023 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.