How to set a custom collider for Entity object in Ursina

74 views Asked by At

I'm currently working on a forest map in a test level in Ursina and i'm using randomiser to spawn fir trees. But i want to decrease their collision box size a bit to be able to go through them as they can spawn close to each other. However, the method i'm trying doesnt seem to work.

Here is my tree class

class tree (Entity):
    def __init__(self, position):
        super().__init__(
            model="Assets/SimpleTree.fbx",
            texture="Assets/Treesnow.png",
            scale=0.007,
            position=position,
            double_sided=True,
            collider='box'
        )

And here i randomly spawn them

#-----RANDOM TREE SPAWN----
for i in range(-100, 100, 10):
    for j in range(-100, 100, 10):
        chance = random()
        if i == player.position[0] and j == player.position[2]:
            chance = 0
        if chance > 0.5:
            spawnTree = tree(position=(i+randint(2, 6), 0, j+randint(2, 6))) #small offset
            #spawnTree.collider = 'box'
            #spawnTree.collider.size=Vec3(spawnTree.position[0]-4, 6, spawnTree.position[2]-4)
            #print(spawnTree.collider.size)

In the comments in the last 3 lines you can see my attempts at directly changing the default size while creating objects, as you can actually address it via 'print()'

Also, I tried using the documentation method but it doesn't change anything if I use the default "box" collider.

if I disable "box" collider in the class and use the line below when spawning, the player has no collision with trees at all.

spawnTree.collider = BoxCollider(spawnTree, center=spawnTree.position, size=Vec3(spawnTree.position[0]-4, 6, spawnTree.position[2]-4))
1

There are 1 answers

0
pokepetter On BEST ANSWER

The BoxCollider approach is correct, but the values are wrong. center is relative to the Entity, so Vec3(0,0,0) would place it at the same place as the tree. size is also relative to the Entity (but not the model). I recommend pressing F10 twice while running the game to be able to see the colliders and adjust this.