Pinball Flippers Control with multitouch
Hi Everybody,
I need an advice.
I have a C# script for “Level Manager” which allows to control two Pinball Flippers prefabs with Ridgidbodies2D (please see attachments). If screen is touched anywhere on the left part, Left Flipper is moving up, same for the right flipper. If touch continues (not released), flipper stays up. As soon as finger is lifted, flipper goes down. I use Gravity setting to avoid shaking of the flipper when holding it up.
Two flippers independently work great and the way I need, however I run into the conflict issue when I try to control both at the same time. it becomes inconsistent and sometime when one finger is touching one side the other side becomes unresponsive or can not control the flipper, controlling both flipper independently at the same time is not perfect or doesn’t work.
Could you please review the script and help me?
Thank you
using UnityEngine;
public class LevelManager : MonoBehaviour {
public float torqueForce;
public GameObject leftFlipper;
public GameObject rightFlipper;
private Rigidbody2D leftFlipperRigid;
private Rigidbody2D rightFlipperRigid;
// Use this for initialization
void Start () {
leftFlipperRigid = leftFlipper.GetComponent<Rigidbody2D>();
rightFlipperRigid = rightFlipper.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mouseInput = Input.mousePosition;
//Flipping right
if (mouseInput.x >= Screen.width / 2f)
{
AddTorque(rightFlipperRigid, -torqueForce);
rightFlipperRigid.gravityScale = 0;
}
//Flipping left
if (mouseInput.x < Screen.width / 2f)
{
AddTorque(leftFlipperRigid, torqueForce);
leftFlipperRigid.gravityScale = 0;
}
}
else if (Input.GetMouseButton(0))
{
Vector3 mouseHolding = Input.mousePosition;
//Holding right
if (mouseHolding.x >= Screen.width / 2f)
{
AddTorque(rightFlipperRigid, -torqueForce);
}
//Holdding left
if (mouseHolding.x < Screen.width / 2f)
{
AddTorque(leftFlipperRigid, torqueForce);
}
}
else if (Input.GetMouseButtonUp(0))
{
Vector3 mouseUp = Input.mousePosition;
//Release right
if (mouseUp.x >= Screen.width / 2f)
{
rightFlipperRigid.gravityScale = 1;
}
//Release left
if (mouseUp.x < Screen.width / 2f)
{
leftFlipperRigid.gravityScale = 1;
}
}
}
void AddTorque(Rigidbody2D rigid, float force)
{
rigid.AddTorque(force);
}
}