pythonrotationursina

Rotating smoothly in python ursina


I'm currently writing a game and in one of my Entity subclasses, I need it to rotate "smoothly" instead of doing a teleporting esque rotation. Right now I have

def smooth_look_at(self, target_position, speed = 10):
    global target_rot
    if not self.pass_: # only set the target rotation and the increment once per thing looked at
        target_rot = (target_position - self.position)
        self.increment = target_rot.y / speed
        self.pass_ = True
    self.rotation.y += self.increment * time.dt #only rotate around the y axis
    if self.rotation == target_rot: #if you reach your rotation goal, return true, reset pass_ for the next time you need to rotate
        self.pass_ = False
        return True

In the update(self) function, walk gets called every time i need it to, and in the walk() function, there is a line :

if not self.smooth_look_at(self.target_position):
    return

so it waits until it finishes turning. what ends up happening is it never turns. What am I doing wrong and how can I fix it?


Solution

  • I suppose I can answer my own question . . .

    For all those who stumble upon this looking for an answer:

    Ursina has a built in function "animate_rotation" as well as some other ones like position, so for my code, it would work like

    self.animate_rotation((x, y, z), duration = .2)