#2D Character controller help

1 messages · Page 1 of 1 (latest)

torpid jay
#

Hello everyone. I've tinkered with Godot, done the GDScript from Zero on the website, and am trying to make my first game. in it you control a mech on the surface of an asteroid. The camera will follow and rotate with the character as it moves around the asteroid. Also, the asteroid won't be that big and most of it will be shown on screen (as shown in the mockup picture).

I kinda get the basics of having my asteroid as an onready variable, finding the centerpoint of it, and having the character pulled toward that point and rotating in relation to it, but actually writing the code is a bit rough.
I've banged my head against the wall enough for tonight, but I was wondering if there was anyone that could provide some insight, or wouldn't mind letting me pick their brain.

ETA: To be clear, I just need help with moving the character along the asteroid.

fathom pawn
#

almost always, the problems need to be broken into the smallest pieces first....I'd just see if you can make the character move while the asteroid stays still first

torpid jay
#

Sorry, should've been clear, the part I'm having issues with is the code for getting it to stick to the asteroid so it can run and jump on the surface.

signal hazel
clever rune
#

you could try to..

  • lock the x velocity of the player to zero
  • use x-axis input to rotate the astroid
  • use y-axis or jump input to add y velocity to player
  • use normal gravity for player (y velocity down)
  • lerp the astroid rotation to slow start and slow stop to fake player start stop velocity
#

..that is, if you want you character to be at the top at all times, so no camera movement needed.. it would be an illusion

signal hazel
clever rune
#

sure but then you have to rotate your inputs too and your gravity and maybe other things

#

gamedev is 50% smoke and 50% mirrors ;)

#

but hey,.. just try and see

signal hazel
clever rune
signal hazel
clever rune
#

If you rotate some vectors, i guess.. as the "left vector" is not going to be left anymore when you are "upside down"

#

gravity is easy, as you just update the gravity vector to the direction of the center of the astroid

#

Something like this i guess..

var gravity_vector := player.global_position.direction_to(planet.global_position)

but there might even be better ways idk 🤷‍♂️

signal hazel
clever rune
#

Was curious.. this sort of works and might help.
It's actually interesting how you only need to rotate one vector once to achive all this magic

extends CharacterBody2D

@onready var planet: StaticBody2D = $"../Planet"
@onready var camera_arm: Node2D = $"../CameraArm"


func _physics_process(delta: float) -> void:
    # degrade velocity
    velocity = lerp(velocity, Vector2(0,0), 0.1)

    # fix player rotation
    look_at(planet.global_position)
    rotation_degrees -= 90

    # init input velocity
    var vel := Vector2.ZERO
    
    # get input
    var input_vector = Input.get_vector("move_left","move_right","move_forward","move_backward")
    vel += input_vector * 8.0
    
    # check for jump
    if Input.is_action_just_pressed("move_jump"):
        var jump_vector = Vector2.UP * 300.0
        vel += jump_vector

    # add gravity
    vel += Vector2.DOWN * 9.2
    
    # rotate inputs velocity to so it matches player rotation
    vel = vel.rotated(rotation)
    
    # add it to the actual velocity
    velocity += vel

    # rotate camera
    camera_arm.global_rotation_degrees = global_rotation_degrees
    
    # do da moving
    move_and_slide()
signal hazel
signal hazel
#

So I tried to do something like what I was mentioning but I had the mobile controls I eas using staying put but otherwise it worked fine.

I think if you parent a camera to the player and then use local vectors all would work fine.

But solution above is nice too

torpid jay
#

Sorry I haven't responded. Had a bit of a personal crisis. I think I'll probably go with rotating the asteroid under the player. Might make for less of a headache. I appreciate all the answers. Love this community.

torpid jay
#

Thank you @signal hazel and @clever rune I really appreciate you guys trouble shooting this for me. I thought this was going to be an easier concept for me to understand and while I'm not awful at math, I have a long way to go with the coding.

signal hazel
clever rune