RedHenDev / ursina_tutorials

MIT License
46 stars 66 forks source link

Player Controller not working #18

Open IsaacPlayzYT opened 1 year ago

IsaacPlayzYT commented 1 year ago

hello I messaged you on YouTube and you told me to past the code here:

mesh_terrain from random import randrange from ursina import *

class MeshTerrain: def init(this):

    this.block = load_model('block.obj')
    this.textureAtlas = 'texture_atlas_3.png'

    this.subsets = []
    this.numSubsets = 1
    this.subWidth = 32

    for i in range(0,this.numSubsets):
        e = Entity( model=Mesh(),
                    texture=this.textureAtlas)
        e.texture_scale*=64/e.texture.width
        this.subsets.append(e)

def genBlock(this,x,y,z):
    # Extend or add to vertices of our model.
    model = this.subsets[0].model

    model.vertices.extend([ Vec3(x,y,z) + v for v in
                            this.block.vertices])

    # This is the texture atlas co-ord for grass :)
    uu = 8
    uv = 7
    model.uvs.extend([Vec2(uu,uv) + u for u in this.block.uvs])

def genTerrain(this):

    x = 0
    z = 0
    y = randrange(-1,1)

    d = int(this.subWidth*0.5)

    for k in range(-d,d):
        for j in range(-d,d):

            this.genBlock(x+k,y,z+j)

        this.subsets[0].model.generate()

main.py

from ursina import * from ursina.prefabs.first_person_controller import FirstPersonController from mesh_terrain import MeshTerrain

app = Ursina()

window.color = color.rgb(200,0,255) subject = FirstPersonController() subject.gravity = 0.0

terrain = MeshTerrain()

def update(): pass pass

terrain.genTerrain()

app.run()

RedHenDev commented 1 year ago

Hi Isaac -- I've recreated your code, and it works.

The only changes I had to make was to correct some indentation and to put double underscores around the init() function in mesh terrain.

Here's the corrected code:

main.py

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from mesh_terrain import MeshTerrain

app = Ursina()

window.color = color.rgb(200,0,255)
subject = FirstPersonController()
subject.gravity = 0.0

terrain = MeshTerrain()

def update():
    pass

terrain.genTerrain()

app.run()

And this is mesh_terrain:

from random import randrange
from ursina import *

class MeshTerrain:

    def __init__(this):

        this.block = load_model('block.obj')
        this.textureAtlas = 'texture_atlas_3.png'

        this.subsets = []
        this.numSubsets = 1
        this.subWidth = 32

        for i in range(0,this.numSubsets):
            e = Entity( model=Mesh(),
                        texture=this.textureAtlas)
            e.texture_scale*=64/e.texture.width
            this.subsets.append(e)

    def genBlock(this,x,y,z):
        # Extend or add to vertices of our model.
        model = this.subsets[0].model

        model.vertices.extend([ Vec3(x,y,z) + v for v in
                                this.block.vertices])

        # This is the texture atlas co-ord for grass :)
        uu = 8
        uv = 7
        model.uvs.extend([Vec2(uu,uv) + u for u in this.block.uvs])

    def genTerrain(this):

        x = 0
        z = 0
        y = randrange(-1,1)

        d = int(this.subWidth*0.5)

        for k in range(-d,d):
            for j in range(-d,d):

                this.genBlock(x+k,y,z+j)

            this.subsets[0].model.generate()

And here's a picture of what your project folder should look like (i.e. includes the correct python files and assets):

Screenshot 2023-01-28 at 09 33 29

IsaacPlayzYT commented 1 year ago

Ok thank you so much

RedHenDev commented 1 year ago

https://github.com/RedHenDev/ursina_tutorials/tree/main/isaac_mc

IsaacPlayzYT commented 1 year ago

Thanks, I'll check it out.

On Sat, 28 Jan 2023 at 09:37, B New @.***> wrote:

https://github.com/RedHenDev/ursina_tutorials/tree/main/isaac_mc

— Reply to this email directly, view it on GitHub https://github.com/RedHenDev/ursina_tutorials/issues/18#issuecomment-1407355694, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5PQOFE7QTPE4KSK2WV6NLDWUTSGVANCNFSM6AAAAAAUH22ME4 . You are receiving this because you authored the thread.Message ID: @.***>