Turn until angle

I need help writing an algorithm that will turns the character until it gets to the right angle for the point. So I have the character a seek actuator, but I want it to but just “face” the object by default but turn manually to face the target and then move again. Any advice on how to do this?

Thanks.

A slightly inexplicit question:

  • Do you want to manually control the player, and if you stop control it, will it turn automatically to a certain object?

Blueprint made you a solution for this in the blender discord server

  • I did not expect your answer (clarification) and decided to write a small script in my understanding - this script probably will be needed in the future (so, a little warm-up):
import bge
from math import hypot, acos, degrees, radians
import mathutils


cont = bge.logic.getCurrentController()
own = cont.owner
keyboard = bge.logic.keyboard


near01 = cont.sensors['near01']
mouse01 = cont.sensors['mouse01']
ray01 = cont.sensors['ray01']


m01 = cont.actuators['mouse_movementsZ']
m02 = cont.actuators['mouse_movementsX']


rotSpeed = own['rotSpeed']


if mouse01.positive:
    cont.activate(m01)
    cont.activate(m02)
else:
    cont.deactivate(m01)
    cont.deactivate(m02)


active = bge.logic.KX_SENSOR_ACTIVE
just = bge.logic.KX_SENSOR_JUST_ACTIVATED


Wkey =  keyboard.events[bge.events.WKEY]
Skey =  keyboard.events[bge.events.SKEY]
Akey =  keyboard.events[bge.events.AKEY]
Dkey =  keyboard.events[bge.events.DKEY]
SPACEBARkey = keyboard.events[bge.events.SPACEKEY]


if Wkey == active:
    if Akey == active:
        own.applyMovement([-0.04, 0.04, 0.0], True)
    elif Dkey == active:
       own.applyMovement([0.04, 0.04, 0.0], True)
    elif not (Akey == active or Dkey == active):
        own.applyMovement([0.0, 0.10, 0.0], True)
elif Skey:
    if Akey == active:
        own.applyMovement([-0.04, -0.04, 0.0], True)
    if Dkey == active:
       own.applyMovement([0.04, -0.04, 0.0], True)
    elif not (Akey == active or Dkey == active):
        own.applyMovement([0.0, -0.10, 0.0], True)
if Dkey == active:
   own.applyMovement([0.08, 0.00, 0.0], True)
if Akey == active:
   own.applyMovement([-0.08, 0.00, 0.0], True)
          
if SPACEBARkey == just and ray01.positive:
    own.applyForce([0.0, 0.0, 350.0], True)  
    
if near01.positive and not mouse01.positive and not (Wkey == active or Skey == active or Akey == active  or Dkey == active or SPACEBARkey == just):
    hitObj = near01.hitObject
    dist = own.getDistanceTo(hitObj)


    lPos01 = (hitObj.position - own.position)
    mat01 = lPos01 * own.worldOrientation
    dist01 = hypot(mat01[0], mat01[1])
    cos01 = acos(mat01[1] / dist01)
    level01 = degrees(cos01)


    if mat01[0] < 0:
        if level01 > rotSpeed:
            own.applyRotation((0.0, 0.0, radians(rotSpeed)), True)
    if mat01[0] > 0:
        if level01 > rotSpeed:
            own.applyRotation((0.0, 0.0, radians(-rotSpeed)), True)


    if own["move"] == True and dist >= 5:
        own.applyMovement((0.0, 0.10, 0.0), True)

example - player_test01.blend (548 KB) - of course, this files is a free extended help on BGE :cool:

  • If in the player’s settings you set move = True, the player will still move to the target.