- Home /
Question by
Paincho · Sep 09, 2014 at 02:53 AM ·
touchscreenpinball
Pinball using touchscreen
Hi I'm using a single script to trigger the flippers depending on where the touch is located on the screen on the x axis. If it's on the right side I trigger the right flipper and the left flipper on the left side. The left side works fine but when I put my finger on the right side of the screen the two flippers both left and right are triggered instead of just the right one. I can't figure out why so if anybody has an idea it would be greatly appreciated. Thanks.
JointSpring L_spring = new JointSpring();
L_spring.spring = L_flipperStrength;
L_spring.damper = L_flipperDamper;
JointSpring R_spring = new JointSpring();
R_spring.spring = R_flipperStrength;
R_spring.damper = R_flipperDamper;
//FLIPPERS MOUSE
if(Input.GetMouseButton(0)){
L_spring.targetPosition = L_pressedPosition;
}else{
L_spring.targetPosition = L_restPosition;
}
if(Input.GetMouseButton(1)){
R_spring.targetPosition = R_pressedPosition;
}else{
R_spring.targetPosition = R_restPosition;
}
//FLIPPERS TOUCH SCREEN
foreach(Touch touch in Input.touches){
if(touch.position.x<posScreen){
if(touch.phase==TouchPhase.Began){
L_spring.targetPosition = L_pressedPosition;
}else{
if(touch.phase==TouchPhase.Stationary){
L_spring.targetPosition = L_pressedPosition;
}else{
if(touch.phase==TouchPhase.Ended){
L_spring.targetPosition = L_restPosition;
}
}
}
}else{
if(touch.phase==TouchPhase.Began){
R_spring.targetPosition = R_pressedPosition;
}else{
if(touch.phase==TouchPhase.Stationary){
R_spring.targetPosition = R_pressedPosition;
}else{
if(touch.phase==TouchPhase.Ended){
R_spring.targetPosition = R_restPosition;
}
}
}
}
}
L_flipper.spring = L_spring;
R_flipper.spring = R_spring;
Comment
Answer by WardPeeters · Apr 05, 2015 at 03:17 PM
Hello
I'm working on a pinball game too. This is the code I'm using for the flippers on mobile devices. Make sure you tick the boolean left paddle true for the left paddle(flipper).
#pragma strict
//Parameters
var restPosition : float = 0F;
var pressedPosition : float = 45F;
var flipperStrength : float = 10F;
var flipperDamper : float = 1F;
var inputButtonName : String = "LeftPaddle";
var isLeftPaddle : boolean = true;
var paddleTouch : boolean = false;
public function Awake ()
{
hingeJoint.useSpring = true;
}
// Update is called once per frame
public function Update ()
{
paddleTouch = false;
var touches : Touch[] = Input.touches;
for (var touch : Touch in Input.touches) {
//if (touch.position && touch.phase != TouchPhase.Canceled)
//Debug.Log(touch.position);
if (isLeftPaddle && touch.position.x < 150){
paddleTouch = true;
}
else if (!isLeftPaddle && touch.position.x >= 150){
paddleTouch = true;
}
}
var spring : JointSpring = new JointSpring();
spring.spring = flipperStrength;
spring.damper = flipperDamper;
if (Input.GetButton(inputButtonName) || paddleTouch){
spring.targetPosition = pressedPosition;
audio.Play();
}else{
spring.targetPosition = restPosition;
}
hingeJoint.spring = spring;
hingeJoint.useLimits = true;
hingeJoint.limits.min = restPosition;
hingeJoint.limits.max = pressedPosition;
}