This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Need help converting C# to GDScript

danjodanjo Posts: 81Member

Hi everyone!

I'm trying to translate this Unity-C#-Script to GDScript
This script implements quake 3 cpm-like movement in Unity, see this video of the original author: link

The basic movement on the ground is working fine but two things cause me some trouble:
1. jumping:
the character just does a minimal jump.. I guess it has to do something with the way that i'm checking if the player is colliding with the floor but i'm not sure
2. the function AirControl:
if I don't move while jumping everything is fine but if do move while jumping playerVelocity.x and playerVelocity.z get very high (or very low depending on direction).
This is what I get in the console while jumping + moving forward (on negative z)

playerVelocity: (-31.217297, 0, -396.653412)
playerVelocity: (-5732674.5, -0.166667, -72840544)
playerVelocity: (-35501225092415464931328, -0.333333, -451085870939387686027264)
ERROR: move: Condition ' p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15 ' is true.
   At: core/math/octree.h:864.
ERROR: _set_transform: Object went too far away (more than 10000000mts from origin).
   At: servers/physics/collision_object_sw.h:90.
ERROR: move: Condition ' p_aabb.pos.x > 1e15 || p_aabb.pos.x < -1e15 ' is true.
   At: core/math/octree.h:864.
playerVelocity: (-inf, -0.5, -inf)
ERROR: _ensure_valid_root: Octree upper size limit reeached, does the AABB supplied contain NAN?
   At: core/math/octree.h:587.
ERROR: move: Condition ' !common_parent ' is true.
   At: core/math/octree.h:957.
playerVelocity: (-inf, -0.666667, -inf)

I pushed a minimal project to github

If I can do anything to make it easier for you to help me, please tell me :smile:


Tags :

Best Answer

  • danjodanjo Posts: 81
    Accepted Answer

    Fixed it. Made a stupid mistake:
    I did velocityPlayer.normalized() instead of playerVelocity = playerVelocity.normalized()

    Here's the diff for anyone who cares:

    diff --git a/scripts/player.gd b/scripts/player.gd
    index 72603d7..228e7d1 100644
    --- a/scripts/player.gd
    +++ b/scripts/player.gd
    @@ -176,8 +176,7 @@ func AirControl(wishdir, wishspeed, delta):
            playerVelocity.y = 0
            # Next two lines are equivalent to idTech's VectorNormalize()
            speed = playerVelocity.length()
    -       playerVelocity.normalized()
    -
    +       playerVelocity = playerVelocity.normalized()
            dot = playerVelocity.dot(wishdir)
            k = 32
            k *= airControl * dot * dot * delta
    @@ -188,7 +187,7 @@ func AirControl(wishdir, wishspeed, delta):
                    playerVelocity.y = playerVelocity.y * speed + wishdir.y * k
                    playerVelocity.z = playerVelocity.z * speed + wishdir.z * k
    
    -               playerVelocity.normalized()
    +               playerVelocity = playerVelocity.normalized()
                    moveDirectionNorm = playerVelocity
    
            playerVelocity.x *= speed
    

    still need to make the jump work but I guess it's better to put this in another question

Answers

  • danjodanjo Posts: 81Member
    Accepted Answer

    Fixed it. Made a stupid mistake:
    I did velocityPlayer.normalized() instead of playerVelocity = playerVelocity.normalized()

    Here's the diff for anyone who cares:

    diff --git a/scripts/player.gd b/scripts/player.gd
    index 72603d7..228e7d1 100644
    --- a/scripts/player.gd
    +++ b/scripts/player.gd
    @@ -176,8 +176,7 @@ func AirControl(wishdir, wishspeed, delta):
            playerVelocity.y = 0
            # Next two lines are equivalent to idTech's VectorNormalize()
            speed = playerVelocity.length()
    -       playerVelocity.normalized()
    -
    +       playerVelocity = playerVelocity.normalized()
            dot = playerVelocity.dot(wishdir)
            k = 32
            k *= airControl * dot * dot * delta
    @@ -188,7 +187,7 @@ func AirControl(wishdir, wishspeed, delta):
                    playerVelocity.y = playerVelocity.y * speed + wishdir.y * k
                    playerVelocity.z = playerVelocity.z * speed + wishdir.z * k
    
    -               playerVelocity.normalized()
    +               playerVelocity = playerVelocity.normalized()
                    moveDirectionNorm = playerVelocity
    
            playerVelocity.x *= speed
    

    still need to make the jump work but I guess it's better to put this in another question

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.