• 3D
  • Character badly reacting to programatically generated ground

I'm facing a strange problem, I have a script that adds the ground blocks using two nested for loops as the scene starts


var tile = {
    "grass" : load("res://scenes/ground/grass.tscn"),
    "rock"  : load("res://scenes/ground/rock.tscn"),
    "sand"  : load("res://scenes/ground/sand.tscn"),
    "swamp" : load("res://scenes/ground/swamp.tscn"),
    "water" : load("res://scenes/ground/water.tscn")
}

func _ready():
    ground = get_node("Ground") as Node
    
    for x in range (-3, 3):
        for z in range (-3, 3):
            var newTile := tile["grass"].instance() as Spatial
            ground.add_child(newTile)
            newTile.transform.origin = Vector3(x, 0, z)

inside this scene, there is also a character positioned just above the ground level, as the scene starts it falls on the ground and I have the result shown in this gif

If I build the ground manually inside the scene instead to create it using those loops, the character falls on the ground and stands still as it should.

What do you think? Why do I have this problem?

Hi,

Could you tell some more about character? Is it KinematicBody or RigidBody or anything else? If it's KinematicBody what code you use to move it?

Also you could try to enable "Visible collision shapes" option to see how it looks with collisions.

The character has a KinematicBody root node, children of this node are a Skeleton node, a MeshInstance, and a CollisionShape (a cylinder). At the moment the character's root node has this script attached

extends KinematicBody

class_name Character

var speed = 2

var space_state : PhysicsDirectSpaceState

var gravity = Vector3.DOWN * 10
var velocity = Vector3.ZERO

func _ready():
    space_state = get_world().direct_space_state

func _physics_process(delta):
    velocity += gravity * delta
    velocity = move_and_slide(velocity, Vector3.UP)

Hmmm, could you try to change coolision shape from cylinder to capsule?

I remember similar issue with cylinder shapes.

I've tried the capsule, it has the same behavior.

Hm, that's interesting. I remember having similar issue when I used cylinder, but later I switched to capsule for my characters and have no issue with that any more. I use grid map for my map, maybe that make difference, but anyway it's strange behavior.

Maybe if you have some minimal reproduction project I could take a look in free time to check if behavior is the same on my side and maybe I figure out something.

The attached zip file contains a project with the least to reproduce the issue. If you run the project as it is, you will see the problem.

If inside the main.gd file you comment the nested for loops and put a "grass.tscn" scene at (0, 0, 0) directly into the scene tree, under the ground node, and run again the project, you'll see the wanted behavior.

@Alhazred said: The attached zip file contains a project with the least to reproduce the issue. If you run the project as it is, you will see the problem.

If inside the main.gd file you comment the nested for loops and put a "grass.tscn" scene at (0, 0, 0) directly into the scene tree, under the ground node, and run again the project, you'll see the wanted behavior.

Hi,

I checked your project and it seems worng behaviour is because your tile is not of size 1x1 it's of size 2x2, so you need to change in your generation:

newTile.transform.origin = Vector3(x * 2, 0, z * 2)

Otherwise tiles are half overlapping each other and that cause strange behaviour of character.

I didn't notice that the tiles were overlapping. That solved the issue, thank you.