Search code examples
pythonpanda3d

3rd Person POV for Game Ursina


I am trying to make an rpg-style game with ursina. I want to have the camera always follow the back of the character. I tried using camera.look_at(player) but I couldn't get the camera to rotate to the back of the character when it rotated.

app = Ursina()

class character(Entity):
    def __init__(self):
        super().__init__(
            model = load_model('cube'),
            color = color.red,
            position = (-0, -3, -8)
        )

player = character()

print(player.forward)

print(player.forward)
camera.look_at(player)
player.rotation_y =180
def update():
    if held_keys['a']:
        player.rotation_y -= 2
    if held_keys['d']:
        player.rotation_y += 2


app.run()```

Solution

  • You may want to change the origin. Also using parents. I'll explain what this means in a moment.

    To change the origin (the point at which an entity is moved and rotated) to a point behind it.

    e.g.

    from ursina import *  # import urisna
    
    app = Ursina()   # make app
    
    player = Entity(model='cube',   # this creates an entity of a cube
                    origin = (0, 0, -2)) #now you have a origin behind the entity
    
    app.run()   #run app
    

    But what about the camera, I hear you ask!

    I'd recommend the ursina.prefabs.first_person_controller

    It may be designed for 1st person control, but you can use it for your purpose.

    # start by doing the normal thing
    from ursina import *
    
    # but also import the first person prefab
    from ursina.prefabs.first_person_controller import FirstPersonController
    
    app = Ursina()
    
    # create camera and player graphic
    cam = FirstPersonController()
    
    player = Entity(model='cube',
                    origin = (0, 0, -2),
                    parent = cam)
    
    # run
    app.run()
    

    You will need to create a floor entity.

    That is ALL YOU NEED for the 3rd person controller. The parents and origins ensure that. It has built in WASD and Arrow Key control, with mouse control too.

    @Cyber-Yosh recently asked a question for this post on how to use it without the 1st person controller. Here's how. I have commented on the changes.

    from ursina import * # import as usual
    app = Ursina()       # create app as usual
    
    window.fps_counter.enabled = False # this is just to remove the fps counter
    
    box = Entity(model='cube',         # create cube as before (you can make your own class)
                 origin=(0,0.7,-5),    # set origin to behind the player and above a little
                 parent=camera,        # make it orientate around the camera
                 color=color.red,      # change the color
                 texture='shore')      # choose a nice texture
    
    def update():                      # define the auto-called update function
        if held_keys['a']:
            camera.rotation_y -= 10 * time.dt # the time.dt thing makes it adapt to the fps so its smooth
        elif held_keys['d']:
            camera.rotation_y += 10 * time.dt
    
    Sky() # just a textured sky to make sure you can see that you are both rotating
    app.run() # run
    

    You'll notice that I've not created a class (adapting this for it is easy enough), but I did not use load_model. This is because even if you are using your own model, you don't need to use load_model. Simply put the name of the file (without the file extension) as a string. This works, I've tried it.

    If you have any more questions, don't hesitate to ask. I am more than happy to help. If this worked, be sure to upvote and approve.