I cannot figure out a basic movment script >.>

Hi All,
So ive been reading up on alot of theory recently about game development and programming etc.
I thought I’d get back into the BGE and try my hand at python, I tried to make a super simply movment script but everytime I use it, my object just spins out of control. It should move similar to the asteroids ship of old.

I’ve tried various fixes and would honestly just like another set of eyes on the code to help me out.

############################################################
###  Make a super simple block game to learn some python ###
############################################################
import GameLogic
import random 


############################################################
cont = GameLogic.getCurrentController()
playerModel = cont.owner


move = cont.actuators["move"]
pressUp = playerModel.sensors["up"]
pressRight = playerModel.sensors["right"]
pressDown = playerModel.sensors["down"]
pressLeft = playerModel.sensors["left"]
pressShift = playerModel.sensors["shift"]
pressCNTRL = playerModel.sensors["cntrl"]

upPressed = False
rightPressed = False
downPressed = False
leftPressed = False


playerRotation =  playerModel.localOrientation.to_euler() 
playerRotationX = playerRotation.x
playerRotationY = playerRotation.y
playerRotationZ = playerRotation.z


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


def changeDirection(y, x , z): 
    rotation = [ y, x, z]
    playerModel.applyRotation( rotation , True )

def updatePosition():
    randomDirection = random.randint(1,360) ## 360 degree
    newRotation = playerRotationZ + randomDirection
    changeDirection(0,0,newRotation)
  
  
 

speed = playerModel["vel"]

if pressUp.positive or pressRight.positive or pressLeft.positive or pressDown.positive:
    if pressUp.positive: ## this will be our "Thrust" button
       playerModel["vel"] = playerModel["vel"] + 0.01
       
    if pressDown.positive: ## our decrease speed 
        playerModel["vel"] = playerModel["vel"] - 0.01
        speed = playerModel["vel"]

    if pressRight.positive: #turn Right
        playerRotationZ = playerRotationZ + 0.1

    if pressLeft.positive: #turn left
        playerRotationZ = playerRotationZ - 0.1
        
    speed = playerModel["vel"]
    move.dLoc = [0, speed, 0.0]        
    move.dRot = [0.0, 0.0, playerRotationZ]
    cont.activate(move)
    print(playerModel.getPosition())


 


  

randomDirection = random.randint(1,360) ## 360 degree
newRotation = playerRotationZ + randomDirection
changeDirection(0,0,newRotation)

What is this code for?

I will look it over better on my computer (I am on my phone)

aha, at first the object would choose a random direction every time the script ran and turn that way.
After I figured out how to do it I just left the code aha

It’s better to use force or linear velocity to move an object rather than Dloc.

Dloc doesn’t work well with collisions, as the object is essentially teleporting each time it moves (even though just by a little bit). You’ll have to make sure the object is a rigid body rather than static physics type (in the physics tab) for the force options to be displayed in the move actuator. Rigid bodies fall down with gravity, so you’ll either need to place a flat plane under your object or switch off gravity (in the world setting tab) if you don’t want it to fall down.

For your script I’d recommend making z rotation a property too, just like velocity rather than trying to pick it up from the localOrientation if you want rotation to be cumulative. Another option is to use torque or angular velocity, which are some of the other force options you can get once the object is a rigid body.

you can use:

if pressRight.positive:
    playerModel['playerRotationZ'] += 0.001
if pressLeft.positive:
    playerModel['playerRotationZ'] += 0.001
    
....

move.dRot = [0.0, 0.0, playerModel['playerRotationZ']]

to get the effect you want. It’s good to use very low settings for rotation as those are radians, not degrees.
approx 3.147 radians = one whole turn.

Also you don’t have to use:

if pressUp.positive or pressRight.positive or pressLeft.positive or pressDown.positive:

before all the sensor checks, though you might want to put it after them to stop extra movement being applied when you shoot or whatever.

I miss the code when you deactivate the actuators

Thanks Smoking, I used all of your advice and now have a very smooth system.