Can I auto correct rotation?

I’m making a shooter with a space ship. I want to be able to roll the ship, but I can’t find a way to make the ship go back to its original orientation after I roll. Is there a way to make it automatically reorient itself? If it requires a python script please tell me exactly how to do it. I’ve never used python before.

I wrote a script for this in my own game

The script is:


######################################################

#                                                    #
#            Written by sdfgeoff for game               #
#                  project "Defender"                #
#                                                    #
#   If you use this script please keep this header   #
######################################################

import bge
cont = bge.getCurrentController
obj = cont.owner


curRot = obj.worldOrientation.to_euler()[1]
#print (curRot)

Turn = obj["Turn"]/15

if curRot < Turn/2 and curRot > -Turn/2:
    Change = 0
elif curRot > 0:
    Change = -Turn
    elif curRot < 0:
    Change = Turn

rotation = [0,Change,0]
obj.applyRotation(rotation, True)

Simply have a property called “Turn” This will be the speed of the autoLevel in radians per 15 frames.
It will only autolevel rolling, not tilt.

Have the following logic:
Always (pulse mode on, time 0) -> Python (script name)

Good luck. Ask back here if you have any problems

Maybe it would be better to create a roll animation for the mesh, rather than rotating the object itself?

My first intention would be an action too.

But you could use the TrackTo actuator to. With a well chosen delay it can track the up-vector back.

BTW: in space there is no “UP” :wink:

The script is coming up with errors and isn’t working. Or I’m doing something wrong with it. I would use an animation, except that sometimes the ship rolls sideways just by pointing it towards the ground and then turning to the side, so I need a way to correct the orientation anyway.

Monster, could you explain your solution a little more.

This is the blend file in case it helps a bit more. It’s made in Blender 2.57.

Attachments

2.57 Ship game.blend (663 KB)

Monster is referring to having an object vertex parented above the ship (so it doesn’t copy rotation), and then having the ship with a track to actuator above it.
This can work.

And I made a few mistakes in that script (I changed it slightly)
Try again with:

######################################################

#                                                    #
#            Written by sdfgeoff for game               #
#                  project "Defender"                #
#                                                    #
#   If you use this script please keep this header   #
######################################################

import bge
cont = bge.logic.getCurrentController()
obj = cont.owner


curRot = obj.worldOrientation.to_euler()[1]
#print (curRot)

Turn = obj["Turn"]/15

if curRot < Turn/2 and curRot > -Turn/2:
    Change = 0
elif curRot > 0:
    Change = -Turn
elif curRot < 0:
    Change = Turn

rotation = [Change,0,0]
obj.applyRotation(rotation, True)

And, set Turn to about 0.125 or so.

Instead of correcting the roll, whenever I tilt the ship, it starts spinning. Also the number I put into the turn property changes the speed of the spin.

I tried the track to actuator and ran into several problems. If this is another solution, could someone explain it a little more fully.

I attached an updated blend file in case it helps.

Attachments

2.57 Ship game.blend (675 KB)

Since I can’t figure out how to make the ship automatically reorient itself, I’ve decided to just manually roll it. Doing it manually actually works fairly well, so I’m just going to stick with that. Thanks for your time though.