Furthermore I wanted to constrain the timer to 2 seconds. Therefore I implemented an if-statement: if Power <= 2:
if power is less than 2???
don’t you mean if power is greater than 2?
…DRot…
Please, [sob] don’t use DRot [sob]… Even if it does work (or it’s replacement, setRotation (I think)) it is a nuicance. If you rotate to close to a wall, you start rotating through. Remember what happens if you use the “loc” setting on logic bricks? It’s lie that.
here’s some new code, with a few important changes:
import bge
cont = bge.logic.getCurrentController()
obj = cont.owner
power = obj["Power"]
print (power)
#Instead of using a timer I used the script
key = cont.sensors["Keyboard"]
#Seeing if the key is pressed, if it is then adding 0.1
if key.triggered:
power = power + 0.1
else:
power = 0
#Maxing it out at 2
if power > 2:
power = 2
#Assigning the property power to it's new value
obj["Power"] = power
#Stuff from your sctipt
powerPercent = power / 0.02
rotationAngle = powerPercent * 0.1
obj["RotationAngle"] = rotationAngle
obj.setAngularVelocity([0, 0, rotationAngle], 1)
Myself I don’t like timers, I find them un-predictable. So instead I use a property that always adds, or in this case, the script decides whether to add or not.
So copy/paste the script and re-wire your keyboard sensor to connect to the python script, and get rid of the “invert.”
Get rid of the toggle logic brick, and make the former timer property into a plain old “float” as well.
If you don’t understand any of the script, then just ask.
There are a few script-writing conventions that you should take note of:
-
“obj”. The cont.owner is ALWAYS either “obj” or “own”. This is just so that you don’t get confused if you run the same script from more than one object, as is the case when games get more complex.
-
camelCode. In all languages, for some stupid reason we have camel-code. What this is is that any property name, object name etc has lower case first letter, and any subsequent words in the name have capitals, so it is “power” or “powerPercent” or “thePowerPercent” or “theAwesomePowerPercent” or maybe “iThinkYouGetTheIdeaNow”. Don’t ask me why this is convention, it just is.
-
only import what you need.
from bge import logic
import math
import bpy
import GameLogic as logic
from math import sqrt
from math import *
from mathutils import *
import mathutils as mt
Keh!!!
This chunk of code imports the same thing quite a few times. It imports math twice, sqrt three times (in the math, and by itself), game logic twice, and mathutils twice.
In general you only need (and in this case) the module “bge”
You use math for complex maths (sqrt, sin, cos, tan, etc)
You use mathutils for vectors
To use part of a module, use:
moduleName.functionName
While there is no harm in importing to many, it just isn’t good practice.
If you want to use “GameLogic” you can replace it with “bge.gameLogic”, saving you importing that module.
I would replace that whole chunk with just:
import bge
import bpy (which can't be used in game I think, just check up on that)
import math
import mathutils
then in a script you can use:
math.sqrt
bge.logic
mathutils.vector
etc
I hope I haven’t put you off here (I can be a bit pedantic), python is a valuable tool, and one that is essential to creating good games in blender.
If you want the modified blend then just ask.