one tap move forward and back?

Game Engine, Players & Web Plug-in, Virtual Reality, support for other engines

Moderators: jesterKing, stiv

zamise
Posts: 15
Joined: Fri May 29, 2009 12:00 am

one tap move forward and back?

Postby zamise » Sun Sep 30, 2012 6:15 pm

Hi I'm new and still learning a lot so not sure if I'll explain this well... I want to press say z once and have the camera move a direction real quick and move back to the same spot real quick. I don't want to have to push a second button to move it back. I tried with a timer property with 0 to .5 on z and then another right after set .5 to 1 and moves it back, but it didn't seem to do it. Thinking it will be something like that or something simple I just don't know. Any help thanks!

Cube
Posts: 4
Joined: Fri Sep 28, 2012 1:59 pm

Postby Cube » Tue Oct 02, 2012 1:42 pm

You could try nesting two for loops inside a while loop (in the code), like this:

while condition == True:
for i in range(x):
*move camera there*
for i in range(x):
*move camera back*

I'm new too and I'm not entirely sure it will work.

zamise
Posts: 15
Joined: Fri May 29, 2009 12:00 am

Postby zamise » Wed Oct 03, 2012 9:34 am

Thanks! I'm not sure where to enter that code for that to work, I've been doing movement in the logic editor. I assume the code should be put in something in there but if elsewhere and then initiated in logic I might need more details on how to figure that out. Thanks again!

Cube
Posts: 4
Joined: Fri Sep 28, 2012 1:59 pm

Postby Cube » Wed Oct 03, 2012 11:28 pm

zamise wrote:I've been doing movement in the logic editor.


In the logic editor, do you have a Python controller set up?

If you don't:
1. de-select everything, (A) and select the camera.
2. With the camera selected, go to the logic editor and create a Python Controller.
3. create a keyboard sensor and assign a key to it and set it to tap.
4. create a motion actuator
5. coonect the Python controller to the keyboard sensor and the motion actuator to the Python controller.
6. open the Text editor and create a new text file called "camera".
7. assign "camera" to the Python controller's script field.
8. type the needed code into "camera".

I'm afraid can't give you code you'll need, as it depends on what you call your keyboard sensors and motion acuator.

This site might help: http://www.cgmasters.net/free-tutorials ... scripting/

With all this done, and the code works, it should move the camera there and back.

zamise
Posts: 15
Joined: Fri May 29, 2009 12:00 am

Postby zamise » Thu Oct 04, 2012 10:51 am

Thanks!

That will take a bit of figuring out for me but giving it a try...

zamise
Posts: 15
Joined: Fri May 29, 2009 12:00 am

Postby zamise » Thu Oct 04, 2012 12:52 pm

Well I got the script to run and then I entered while loops but the camera just sits there. I get the feeling I'm either doing the while loop wrong or it doesn't update the motion every loop. Any more ideas?

Thanks.

Script looks like this:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
move = cont.actuators["move"]
pressup = cont.sensors["z"]
speed = move.dLoc[1]


if pressup.positive:
count = 0
while count < 20:
speed = speed - 0.05
move.dLoc = [0.0, 0.0, -speed]
cont.activate(move)
count = count + 1

count = 0
while count < 20:
speed = speed - 0.05
move.dLoc = [0.0, 0.0, speed]
cont.activate(move)
count = count + 1

Cube
Posts: 4
Joined: Fri Sep 28, 2012 1:59 pm

Postby Cube » Thu Oct 04, 2012 1:37 pm

Hmm...

I found some problems with your code, they might be holding the camera back.

1. move.dLoc = [0.0, 0.0, -speed] should be: move.dLoc = [0.0, speed, 0,0]
the "1" in "speed = move.dLoc[1]" refers to the second item in the list, so your directions weren't matching up. plus "-speed" is implying subtraction.

2. is your code properly indented? Make sure to indent one tab space after every loop starting line and un-indent when the loop is over. Don't indent with the space bar!

3. in the first loop, "speed = speed - 0.05" should be "speed = speed + 0.05".

zamise
Posts: 15
Joined: Fri May 29, 2009 12:00 am

Postby zamise » Thu Oct 04, 2012 6:48 pm

I got it to move the camera with some changes thanks to your help. Problem is it seems it only uses the last loop or last calculation instead of looping through it several sending each loop to the actuator to move. I've a feeling it is just using the last loop, or last setting or something like going through it too fast to see a back and forth movement. Not sure what is going on but here is latest code:

Code: Select all

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
move = cont.actuators["move"]
pressup = cont.sensors["z"]
speed = move.dLoc[2]


if pressup.positive:
    count = 0
    while count < 20:
        speed = speed + 0.05
        move.dLoc = [0.0, 0.0, -speed]
        cont.activate(move)
        count = count + 1

    count = 0
    while count < 20:
        speed = speed + 0.05
        move.dLoc = [0.0, 0.0, speed]
        cont.activate(move)
        count = count + 1


The negatives are the direction I want it go, those are ok as far as I know cause it supposed to go the opposite direction same amount. It should actually not move when the code is done. As is it is moving in the direction of the second loop and stays moving.

Any more ideas? Way to slow the loop down a bit so can see go through the movements? Or is it not going to happen this way?

Thanks for the help guys, even if it don't work I've learned some interesting stuff.[/code]

zamise
Posts: 15
Joined: Fri May 29, 2009 12:00 am

Postby zamise » Thu Oct 04, 2012 6:53 pm

P.S. the idea is to have the camera look like it is shaking a little bit each time a kick drum happens via a midi translator I have sending to the keyboard. I've already got the graphics in the game engine and it works pretty well, but I noticed when I zoon in and out real quick manually with the mouse to the beat it looks pretty cool so I wanted to mimic that automatically.


Return to “Interactive 3d”

Who is online

Users browsing this forum: No registered users and 3 guests