#1
|
|||
|
|||
moving object forward backward
When the mouse is pointed at the object I want to move it depending on wich key i press. A normal int number works but example +=0.5 does not work.
Code: Code:
cube = vizshape.addCube() def moveForward(): object = viz.pick() if object.valid(): pos += 10 object.setPosition([pos],0,0) print 'move backward' vizact.onkeydown( 'w', moveForward ) def moveBackward (): object = viz.pick() if object.valid(): pos += 10 object.setPosition([pos],0,0) print 'move forward' vizact.onkeydown( 's', moveBackward ) |
#2
|
|||
|
|||
hello,
the setPosition command accepts lists in [x,y,z] format so try object.setPosition([pos,0,0]). if you're trying to go forward and backward, that's normally along the z-axis so you might want to change it to ([0,0,pos]). Masaki |
#3
|
|||
|
|||
That is not working. This is the error I get:
Traceback (most recent call last): File "C:\Program Files (x86)\WorldViz\Vizard30/python\vizact.py", line 3019, in __onkeydown self._callGroup(self.__keydownmap[key]) File "C:\Program Files (x86)\WorldViz\Vizard30/python\vizact.py", line 2971, in _callGroup val = e.call(arg) File "C:\Program Files (x86)\WorldViz\Vizard30/python\vizact.py", line 2794, in _callStatic return func(*args,**kwargs) File "Test wall.py", line 39, in moveForward pos += 0.5 UnboundLocalError: local variable 'pos' referenced before assignment My code: Code:
def moveForward(): object = viz.pick() pos += 0.5 if object.valid(): object.setPosition([pos,0,0]) viz.callback(viz.COLLIDE_BEGIN_EVENT,oncollide) vizact.onkeydown( 'w', moveForward |
#4
|
|||
|
|||
You need to assign a value to pos before you add to it:
Code:
import viz import vizshape import vizact viz.go() cube = vizshape.addCube() cube.setPosition([0,1.5,3]) pos = 3 def moveForward(): global pos object = viz.pick() if object.valid(): pos += 0.5 object.setPosition([0,1.5,pos]) print 'move forward' vizact.onkeydown( 'w', moveForward ) def moveBackward (): global pos object = viz.pick() if object.valid(): pos -= 0.5 object.setPosition([0,1.5,pos]) print 'move backward' vizact.onkeydown( 's', moveBackward ) |
#5
|
|||
|
|||
Tnx it worked. Tnx for your time
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
retrieve Object names | Geoffrey | Vizard | 11 | 12-11-2009 05:26 AM |
Attaching sound to a moving object | GiudiceLab | Vizard | 6 | 08-21-2009 09:52 AM |
moving and object by mouse but don't know how to stop the movement | nlfrnassimi | Vizard | 8 | 04-26-2009 08:23 AM |
Moving view with object | Xliben | Vizard | 2 | 07-25-2005 06:36 PM |