0
\$\begingroup\$

I am using Godot 4.3, it is giving me this error and when I change 0 to 0.0, the character stops moving. If I change the last parameter to an integer also it still shoots an error:

extends CharacterBody3D


var myVelocity = Vector3(0,0,0)
const SPEED = 6

func _ready() -> void:
    pass # Replace with function body.

func _physics_process(delta: float) -> void:
    
    if Input.is_action_pressed("ui_right") and Input.is_action_pressed("ui_left"):
        velocity.x = 0
    elif Input.is_action_pressed("ui_right"):
        myVelocity.x = SPEED
    elif Input.is_action_pressed("ui_left"):
        myVelocity.x = -SPEED
    else:
        myVelocity.z = lerp(myVelocity.z,0,0.1)

        
    if Input.is_action_pressed("ui_up") and Input.is_action_pressed("ui_down"):
        velocity.z = 0
    elif Input.is_action_pressed("ui_up"):
        myVelocity.z = -SPEED
    elif Input.is_action_pressed("ui_down"):
        myVelocity.z = SPEED
    else:
        myVelocity.z = lerp(myVelocity.z,0,0.1)

        
    velocity = myVelocity
    move_and_slide()
\$\endgroup\$
2
  • \$\begingroup\$ I copy-pasted your code in a sample project and (after fixing the errors by replacing 0 with 0.0) it seems to work and the character moves fine. \$\endgroup\$ Commented Dec 2, 2024 at 15:46
  • \$\begingroup\$ Instead of lerping velocity.z to 0, How to Lerp Correctly explains why you probably want to use 1.0 - pow(0.5, delta * lerp_speed) instead of 0.1 as the last argument to lerp(). \$\endgroup\$ Commented Jan 3 at 2:07

1 Answer 1

3
\$\begingroup\$

From the docs:

Both from and to must be the same type.

Since a Vector3's components are of type float the second argument to lerp also has to be a float: lerp(myVelocity.z, 0.0, 0.1).

As for this:

when i change 0 to 0.0, the character stops moving

If your character stops moving even when you are pressing a movement key that is probably due to some other bug. Try removing one piece of code at a time and see which piece of code is responsible for this behaviour.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks. I'll try it out \$\endgroup\$ Commented Dec 1, 2024 at 9:11
  • \$\begingroup\$ @zikorachimnefefredrick Did this answer solve your issue? If so, you should mark it as accepted. \$\endgroup\$ Commented Jan 4 at 16:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.