Search code examples
pythonursina

Change texture of entity when you hover on it


I want make a code which is changes the texture of an entity when you hover on it. How to do it? Suddenly, class Entity doesn't have a "highlight_texture" argument. I didn't find any questions with it on this website.

My code of my entity:

from ursina import *

app = Ursina()

class Door(Entity):
    def __init__(self, position=(0, 0, 0), blocked=False, parent=None):
        super().__init__(
            model='cube',
            position=position,
            texture=load_texture('assets/door'),
            scale=(2, 3.5, .1)
        )

        # This door must change its texture to 'assets/door_hovered'.

Solution

  • You can manually change door texture on mouse hover. Below is the simple example:

    import ursina
    from ursina.prefabs.first_person_controller import FirstPersonController
    
    app = ursina.Ursina()
    FirstPersonController()
    floor = []
    for x in range(10):
        for y in range(10):
            floor.append(ursina.Entity(parent=ursina.scene, model='cube', position=(x, -10, y), texture='grass', collider='box'))
    
    door = ursina.Entity(parent=ursina.scene, model='cube', position=(5, -9, 5), texture='door', collider='box')
    floor.append(door)
    
    
    def update():
        if ursina.mouse.hovered_entity == door:
            door.texture = 'door_hover'
        else:
            door.texture = 'door'
    
    
    app.run()
    

    hover