#okay im lost idk what i did

1 messages · Page 1 of 1 (latest)

halcyon halo
#

extends CharacterBody2D

const max_speed = 400
const accel = 500
const friction = 500

var input = Vector2.ZERO
func _physics_process(delta):
player_movement(delta)

func get_input():
input.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
input.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
return input.normalized()

func player_movement(delta):
input = get_input()

if input == Vector2.ZERO:
    if velocity.length() > (friction * delta):
        velocity -= velocity.normalized() * (friction * delta)
    else:
        velocity = Vector2.ZERO
else:
    velocity += (input * accel * delta)
    velocity = velocity.limit_length(max_speed)
    
move_and_slide()
#

thats the whols script so far

valid bladeBOT
#
Embedding code in discord messages
Inline Code

When you surround some words with single backticks like `this`, it will be formatted as code.

Code Blocks

You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:

print("hello world")
Upload

If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/

paper raptor
#

in the Project Settings you can edit the Input Map

#

have you created new entries for "left", "right",... instead of using the existing "ui_left", "ui_right", ...?

halcyon halo
paper raptor
#

then you have to use the names of the new actions you created

func player_movement(delta):
    input = Input.get_vector("left", "right", "up", "down")
halcyon halo
#

sorry im extremly new to this

lyric obsidian
#

You need to replace: func player_movement(delta): input = get_input()

paper raptor
#

you don't need the get_input() method at all. I showed you how you could sinplify it

lyric obsidian
#

With this: #1305111518605611061 message

halcyon halo
#

okay so what does the get_input(): do differently that specifying to use my custom inputs?

lyric obsidian
#

This method also accounts for the "strength" of the pressing (like when you use a controller and are not pushing the thumb all the way)

halcyon halo
#

well of course doign the change that parapixel said works hahaha thanks for that

lyric obsidian
#

And the issue with your implementation is that you only have 1 or 0. As is_action_pressed can either be false or truth

#

However the movement to the left -1 and right is 1

#

So the value range is incorrect

halcyon halo
#

and not just stopping as soon as i unpress the button

lyric obsidian
#

If you want to leverage acceleration & deacceleration. You rather want to use lerp. And feed in the current velocity and your target velocity + some acceleration/friction value

#

Due to the lerp, it will take some time to reach the maximum speed and also take some time before it stops

halcyon halo
#

okay so deffinetly never used lerp before so i have 0 idea on how that is coded

lyric obsidian
#

I don't have GD Script coding laying around for that, however I do have some C# Code

#
        velocity = velocity.Lerp(desiredVelocity, (float)(1 - Mathf.Exp(-Acceleration * GetProcessDeltaTime())));```
halcyon halo
#

right now my chara does come to a slower speed before stopping completly but its not a "smooth" transition

lyric obsidian
halcyon halo
#

var desiredVelocity = direction * MaxSpeed;
velocity = velocity.Lerp(desiredVelocity, (float)(1 - Mathf.Exp(-Acceleration * GetProcessDeltaTime())));

#

where woudl all of this go

#

cause it looks liek to me this would all go under func player_movement(delta):

lyric obsidian
#
    # This is already normalized
    var direction = Input.get_vector("left", "right", "up", "down")

    var desiredVelocity = direction * MaxSpeed;
    velocity = velocity.Lerp(desiredVelocity, (float)(1 - Mathf.Exp(-Acceleration * GetProcessDeltaTime())));

    move_and_slide() # This method uses delta under the hood so no need to actively use that```
#

MaxSpeed would need to be provided as class variable, same as Acceleration

#

For my game i have speed 90 and Acceleration on 25

#

But you can play around with the values, until they feel right for you

halcyon halo
#

okay so i added all of that and im getting identififier "direction" not declared

lyric obsidian
#

Did you rename your input to direction as well :?

halcyon halo
#

also my MaxSpeed is spelled max_speed and i see that becomign a problem as well

lyric obsidian
#

Like I said I took my C# Code, so you will need to adjust the variables to what you have. For GDScript it is common to name them like you do

halcyon halo
#

ohhh i missed that my fault i didnt catch that you said c#

lyric obsidian
#

Just to clarify Mathf.Exp will also not be available to you, and probably you won't need to make a float cast. As in C# this method returns double

#

Seems like your method is exp

halcyon halo
#

well i do thank both of you for this info, now to play around with it to make it work in GDscript

lyric obsidian
#

👍

halcyon halo
#

quick question

#

base self

#

i forget what that means

paper raptor
halcyon halo
#

XXXX not found in base self

paper raptor
#

not found in the current object (the script is attached to)

halcyon halo
#

okay thats a way better way to say it than using all the technically jargon

#

thanks

#

extends CharacterBody2D

const max_speed = 400
const accel = 500
const friction = 500

var input = Vector2.ZERO
func _physics_process(delta):
player_movement(delta)
if Input.is_action_pressed("input"):
velocity = lerp(velocity, Vector2(max_speed, 0), 0.9)
func get_input():
input.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
input.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
return input.normalized()

func player_movement(delta):
input = Input.get_vector("left", "right", "up", "down")

var desiredVelocity = input * max_speed;

if input == Vector2.ZERO:
    if velocity.length() > (friction * delta):
        velocity -= velocity.normalized() * (friction * delta)
    else:
        velocity = Vector2.ZERO
else:
    velocity += (input * accel * delta)
    velocity = velocity.limit_length(max_speed)
    
move_and_slide()
#

okay so i made it work with all this right here