adding scaling increments

not sure if this post should be here or in the python section, but i am trying to make a targets objects Z scale go up in increments of .5 until its total Z scale is 10 a ‘cube’ comes close to it, then the target objects scale with go back to its original scale in increments of .5 when the object moves away.

import bge

def main():

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


if not own.localScale in own:

    own.localScale = ([1,1,1])

scene = bge.logic.getCurrentScene()
target = scene.objects["Cube"]
dist = (	own.getDistanceTo(target))


Scaler = .5

if dist<.5:
    if  own.localScale.z <10:
         own.localScale.z += Scaler
else:
    if  own.localScale.z >1:
         own.localScale.z -= Scaler

main()

this is the script up to now, but it only adds one .5 to the scale of the target object then takes it away when the ‘cube’ moves away. i cannot seem to find a way to apply multiple .5 increments till it reaches the correct size. can anyone help?

Please use code tags to wrap your code bits so we can see the formatting correctly.


if not own.localScale in own:

own.localScale = ([1,1,1])

There will always be .localScale attribute to all game objects and it’s set to what you have as the object’s scale when you start BGE.

Here is also the error. You are checking for own.localScale (which is a list [1.0, 1.0, 1.0]) in own where you tried to search for localScale (which is a name for an attribute and also not worth checking since it’s always there). The check for [1.0, 1.0, 1.0] in a game object is never going to find a match so the script sets own.localScale every tick.

So to fix, just remove this part.

I get a few problems here:

1 . You are comparing an integer with a float. So, first set

<i>
if own.localScale.z&lt; 10.0:
</i>
  1. You are adding to the scale and not multiplying. So you should put

own.localScale[2]+= own.localScale[2]*0.5

If I understood correctly, you want the object to scale up by half its scale… errr, why just the z axis???

  1. That’s inconsistent but not a problem in python. You can in fact do equality check as well:

print(1 == 1.0)
True

  1. Frankly he says he wants the scale to go up in 0.5 increments and it’s what his script is set to do. Also the x,y,z naming should work for scale.
1 Like

thanks! that has got it working.
am no good with scripting and only started using blender its self 2 weeks ago never mind python.

i wanted to scale in the z so i could make a window shutter. i had it working using a scale animation, but when i tried to save the animation from game to use in the renderer there was major lag between the window shutters opening and closing and the movement of the cube. using a script removes the lag

You can even scale with an action. Create an action with scale keys and play with the action actuator in property mode.