Hey guys,
I’m making a mini golf game and right now I just have a cube that lurches forward to collide with the ball, there’s not variability in the shot power. I’m looking for a way to have that cube perform the same action, but move forward based on the length of the keypress to simulate variable power (a quick press = light tap on the ball…long press = hard hit)
I know this is possible because I’ve seen other blender games that do such a thing. Any ideas?
Here’s my .blend file if you wanna have a look
https://drive.google.com/file/d/0BxB2pKMjGzzqUzNxdDN6bThhb1U/view?usp=sharing
You just need a little bit of analytical thinking. Especially about timing.
I usually try to ask “When happens what”. In most of the cases the answers scale very well into the BGE’s SCA as this is what you configure there:
When (the sensor measures the situation) -> trigger What the actuators should do.
But first things first:
Variable power
When happens what? you already said it:
The when is: length of keypress is known
The what is: simulate variable [dependent] power
There is no sensor to determine when the length of the keypress is known. So we need to look if we can use other sensors and operations to sense that situation.
I would look into how to sense the length of keypress.
Length of Keypress
The same question appears:
When happens what?
I would measure the time between keypress and keyrelease and define that as length of keypress.
Keypress and keyrelease can be sensed by a keyboard, a mousekey or a joysticksensor.
Keypress is positive check key release is negative check.
Calculating the length has several options, from property counter, to timer property other to realtime counter (Python). For simplicity I choose timer property as it counts automatically.
[th]Property Name[/th]
[th]Type[/th]
[th]Value[/th]
keypressTimer
Timer
0.0
keypressLength
Float
0.0
[th]Sensor [/th]
[th]Controller[/th]
[th]Actuator[/th]
Keyboard sensor [No Level Triggering]
AND
Property Actuator Mode: Assign Property: keypressTimer Value: 0.0
NOR
Property Actuator Mode: Assign Property: keypressLength Value: keypressTimer
So now we have the result we are looking for in the property keypressLength.
So we can observe this property. When it changes from 0.0 to any value we know it was calculated (assuming you reset it too zero before you start with the next length measure).
[th]sensor [/th]
[th]controller[/th]
[th]actuator[/th]
Property sensor [No Level Triggering]
Mode: equal Property: keypressLength value: 0.0
Python that translates property keypressLength into whatever you want (e.g. a force)
whatever you like (do not forget to activate them)
I hope it helps
Monster
Thank you so much for that explanation! I’ve gone through and implemented everything, however, I’m having trouble translating that keypressLength value into forward motion. I’ve tried using several different actuators, but I can’t find anything that might let me apply the value of keypressLength into any kind of motion.
Attachments
I figured it out! Kind of…I fixed my python script and bypassed the keypresslength calculator just to test my script and it worked. The only thing that’s not working for me now is the keypresslength calculation.
[TABLE=“class: cms_table”]
[TH=“align: center”]Sensor[/TH]
[TH=“align: center”]Controller[/TH]
[TH=“align: center”]Actuator[/TH]
Keyboard sensor [No Level Triggering]
AND
Property Actuator Mode: Assign Property: keypressTimer Value: 0.0
NOR
Property Actuator Mode: Assign Property: keypressLength Value: keypressTimer
[/TABLE]
[TABLE=“class: cms_table”]
[TH=“align: center”]Sensor[/TH]
[TH=“align: center”]Controller[/TH]
[TH=“align: center”]Actuator[/TH]
Keyboard sensor [No Level Triggering]
AND
Property Actuator Mode: Assign Property: keypressTimer Value: 0.0
NOR
Property Actuator Mode: Assign Property: keypressLength Value: keypressTimer
[/TABLE]
Is this one sensor split to two controllers, each to their own actuator? Like in my picture above? That’s how I have it set up now and it doesn’t seem to be working
I can make that a little more understandable…
A property must be set up to record speed_value (a user defined variable).
speed_value - 0.0
While the key is pressed, it sends a TRUE signal, AND
speed_value increases by n with each TRUE signal received.
While the keypress is FALSE, speed_value = 0.0
Here’s what the set-up looks like:
Attachments
The next question is how to change the pitch of a sound file to make a realistic engine revving sound.