#Barrel Roll

21 messages · Page 1 of 1 (latest)

inner sun
#

Hey everyone, game dev noobie here hoping to get some help adding a barrel roll to my project.

This is my current implementation using lerp, but it only works sometimes... wondering if there's a better way.

    var rotation = deg_to_rad(720.0)
    ship.rotation.z = lerp(ship.rotation.z, ship.rotation.z+rotation, 0.3)
    coll.rotation.z = lerp(coll.rotation.z, coll.rotation.z+rotation, 0.3)```

I also use this when moving left or right to give the ship some lean, which is where the animation kind of falls apart:

```    var direction = Vector3(Input.get_axis("move_left", "move_right"), 0, 0)
    velocity = direction * speed
    if direction:
        last_direction = direction
        velocity.z = direction.z * speed
        ship.rotation.z = lerp_angle(ship.rotation.z, -atan2(-last_direction.x, -last_direction.z), delta * rotation_speed)
        coll.rotation.z = lerp_angle(coll.rotation.z, -atan2(-last_direction.x, -last_direction.z), delta * rotation_speed)
    else:
        velocity.z = move_toward(velocity.z, 0, speed)
        ship.rotation.z = lerp_angle(ship.rotation.z, 0, 0.1)
        coll.rotation.z = lerp_angle(ship.rotation.z, 0, 0.1)
    move_and_slide()```

I attached a video showing the roll, then moving while rolling, then just moving, then moving while rolling again. I'd love to get to a more consistent solution that rolls regardless of my position, and that I can easily control how many times the ship spins. I'm also planning on adding some "wind" around the character when they spin, so is lerp even the right thing to be using here?
true gale
#

I would probably have a variable which is a “target” rotation. And then constantly lerp or tween towards that. Your current functions lerp towards constantly changing values from multiple places

inner sun
true gale
#

so for the types of movements you want in your game, you want the ship to "bank" when it moves left and right normally?

#

and then additionally, you want to be able to have a barrel roll on top, which is kind of like an animation that plays?

#

x number of spins?

inner sun
#

Yeah, the same type of movement a jet makes when it turns basically

true gale
#

what should it look like when you are barrel rolling and moving left?

inner sun
#

rolling back to it's previous position

#

if still holding left or right

true gale
#

okay yeah I think I get it

#

so yeah what I would do is something like:

#

[pseudo code]

var target_angle : float = 0

if holding left: 
- target_angle = -45

elif holding right:
- target_angle = 45

elif doing barrel roll left:
- target_angle -= 5  [while a 1 second timer is active]

[repeat for barrel roll right]


_process():
ship.angle.lerp(target_angle, ... )

#

pseudo code didn't really help here haha. But the idea is that you set some "target angle" based on whatever action you're doing. And then you constantly lerp towards that. Rather than trying to update the exact angle in each action

inner sun
#

I see what you mean, though is this considering the roll itself? Sorry maybe I explained badly but I want the ship to shift angle when it's going left or right, but when the roll button is hit I want it to quickly spin a few times

true gale
#

yeah that makes sense

#

extends CharacterBody3D


const SPEED = 5.0

const BANK_SPEED = 5.0
var bank_angle : float = 0
var extra_angle : float = 0

@onready var ship_mesh : CSGBox3D = $CSGBox3D

func update_bank(delta : float):
    var target_angle = bank_angle + extra_angle
    ship_mesh.rotation_degrees.z = rad_to_deg(lerp_angle(deg_to_rad(ship_mesh.rotation_degrees.z), deg_to_rad(target_angle), delta * BANK_SPEED))

func _physics_process(delta: float) -> void:

    var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
    var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    
    #barrel roll left 360 degrees
    if Input.is_key_pressed(KEY_SPACE):
        extra_angle = 0
        var tween = create_tween()
        tween.tween_property(self,"extra_angle",360,0.5)
        #tween.tween_property(self,"extra_angle",0,0) #reset to 0 on tween end
    
    bank_angle = direction.x * 35
    
    if direction:
        velocity.x = direction.x * SPEED
        velocity.z = direction.z * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
        velocity.z = move_toward(velocity.z, 0, SPEED)

    update_bank(delta)
    move_and_slide()


I've done some nasty code with the radians/lerping but this is the sort of setup I would use for simple movement

inner sun
true gale
#

looks great! I like the art style

inner sun
#

thanks haha, needs a lot of work but I want to get a solid game loop first