Turning Mechanics

So I’ve been trying to optimize my game graphically, and I took the idea of the head rotating, and the player moves to where the head is rotated. It’s to make my game look a lot better, but unfortunately: I tried to make the neck rotation empty and the player have the same z-axis orientation, but that failed. The head also has an IK, so the torso moves with the head, just for your information.

Here’s the code I use:


from bge import logic


scene = logic.getCurrentScene()
cont = logic.getCurrentController()
own = logic.getCurrentController().owner


NeckRotation = scene.objects['NeckRotation']
Player = scene.objects['PlayerRig']


#Setting Positions


NeckRotation.localOrientation[2] = Player.localOrientation[2]

Sorry if this seems vague, but it’s the best way I can explain things. I might also get into animations later on, in this same thread, so I’m not spamming this website.

object.worldOrientation = object.worldOrientation.lerp(head.worldOrientation,.5)
object.alignAxisToVect([0,0,1],2,1)

(lerp to other while facing up on z*)

What’s lerp? Haha, sorry just wondering.

Also, this is with two different objects.

https://docs.blender.org/api/blender_python_api_2_62_release/mathutils.html#mathutils.Matrix.lerp

Mind if I ask what that’s for?

keep the body pointing z axis up

Hi there.
I was walking around, and decided to help a little:

import bge
import math
from math import degrees

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

scene = bge.logic.getCurrentScene()

NeckRotation = scene.objects['NeckRotation']
Player = scene.objects['PlayerRig']

xyz = own.localOrientation.to_euler()

NeckRotation.localOrientation = xyz

Code tested, everything is working.

I appreciate the code there, but unfortunately: The entire player isn’t facing the z axis properly. So BluePrintRandom’s scripts seem to do the job. As again, I appreciate your help.

  • no problem, man.

Alright, so I thank you guys very much for your support and all, but like I stated: I will add more to this, so I’m not spamming the thread with reiterated questions.

So the scripted created by BluePrintRandom worked efficiently, after some minor tempering. Now I made it so the entire player faces the neck rotation when the player moves, or when it does a complete turn. I’ve got the moving segment down, but apparently, I’m having a bit of a struggle on the turning. Here’s what I have so far, and the bolded is the turning segment:



from bge import logic, events
import math




scene = logic.getCurrentScene()
cont = logic.getCurrentController()
own = logic.getCurrentController().owner


NeckRotation = scene.objects['NeckRotation']
Player = scene.objects['PlayerRig']
JumpSensor = scene.objects['PlayerJumpSensor']


#Key Actuators
key_none = logic.KX_INPUT_NONE
key_tap = logic.KX_INPUT_JUST_ACTIVATED
key_active = logic.KX_INPUT_ACTIVE


#Movement Keys


UPARROWKEY = logic.keyboard.events[events.UPARROWKEY]
DOWNARROWKEY = logic.keyboard.events[events.DOWNARROWKEY]
RIGHTARROWKEY = logic.keyboard.events[events.RIGHTARROWKEY]
LEFTARROWKEY = logic.keyboard.events[events.LEFTARROWKEY]
    
if UPARROWKEY or DOWNARROWKEY or RIGHTARROWKEY or LEFTARROWKEY == key_active:
    Movement = True
    


#Setting Positions


<b>NeckOrientation = NeckRotation.worldOrientation.to_euler()</b>

<b>if NeckOrientation.z &gt;= 10: </b>

<b>    Player.localOrientation = Player.localOrientation.lerp(NeckRotation.localOrientation, 0.01)</b>

<b>    Player.alignAxisToVect([0, 0, 1], 2, 1)</b>


if Movement == True and JumpSensor["Grounded"] == True:
    
    Player.localOrientation = Player.localOrientation.lerp(NeckRotation.localOrientation, 0.10)
    
    Player.alignAxisToVect([0, 0, 1], 2, 1)



Might as well inform you guys that I currently built it to make it turn for one side, so I can duplicate the same process by incorporating an “and…” along with the contrapositive (or inverse?). Once again, thanks.