Search Unity

2d Pinball Game - How to keep the flippers up

Discussion in '2D' started by CarreraSilvio, Feb 17, 2015.

  1. CarreraSilvio

    CarreraSilvio

    Joined:
    Jul 21, 2012
    Posts:
    31
    Hey guys I've been toying with a pinball game on unity for a while. It's been ok so far but I can't seem to get the flippers to remain up if the key is being held. I'm using 2D hinge joints to do the trick but I've tried messing with the limits but that doesn't seem to work quite as I intended.

    Any advice?

    Link for the prototype

    Thanks
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Why can't you continue using whatever method you're already using to move the flippers when the key is held? Either adding torque or using the hinge-joint motor.
     
  3. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    here is my script for my flippers in my pinball game , dont look at the values for force/velocity as these are probably tweaked in the inspector. It so you can see how to use the hinge motors :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FlipperCapsule : MonoBehaviour {
    6.  
    7.  
    8.     private HingeJoint joint;
    9.     private JointMotor jointMotor1;
    10.     private JointMotor jointMotor2;
    11.     private JointLimits limits;
    12.     //targetVelocity is degrees per second
    13.     public float targetVelocity = 2500;
    14.     public float rotationMax = 70;
    15.     public float force = 1900;
    16.     public float rotationPolarity = 1;
    17.     public KeyCode keyCode;
    18.     public Rigidbody connectedBody;
    19.     private bool currentActive = false;
    20.  
    21.     public AudioClip sfx;
    22.  
    23.     void Start () {
    24.      
    25.  
    26.         rigidbody.maxAngularVelocity = 10000;
    27.      
    28.         joint = gameObject.AddComponent<HingeJoint>();
    29.         joint.connectedBody = connectedBody;
    30.         joint.axis = Vector3.up;
    31.         jointMotor1 = joint.motor;
    32.         jointMotor1.freeSpin = false;    
    33.         jointMotor1.force = force;
    34.           jointMotor1.targetVelocity = targetVelocity*rotationPolarity;
    35.      
    36.         jointMotor2 = joint.motor;
    37.         jointMotor2.freeSpin = false;    
    38.         jointMotor2.force = force;
    39.           jointMotor2.targetVelocity = targetVelocity*-1*rotationPolarity;
    40.      
    41.      
    42.      
    43.         float tempLimit = rotationMax*rotationPolarity;
    44.      
    45.         limits = joint.limits;
    46.         if(tempLimit < 0){
    47.             limits.min = tempLimit;
    48.             limits.max = 0;
    49.         }else{
    50.             limits.min = 0;
    51.             limits.max = tempLimit;
    52.          
    53.         };
    54.        
    55.         limits.minBounce = 0;
    56.      
    57.         limits.maxBounce = 0;
    58.        joint.limits = limits;
    59.      
    60.         joint.useLimits = true;
    61.      
    62.      
    63.         rigidbody.centerOfMass = Vector3.zero;
    64.      
    65.         joint.motor = jointMotor2;
    66.      
    67.      
    68.     }
    69.  
    70.  
    71.  
    72.  
    73.  
    74.     public void Animate(bool makeActive){
    75.         if(currentActive != makeActive){
    76.             currentActive = makeActive;
    77.             if(currentActive){
    78.                 joint.motor = jointMotor1;
    79.                 AudioManager.PlayAudioOneShot(sfx);
    80.                  
    81.             }else{
    82.                 joint.motor = jointMotor2;
    83.             };
    84.          
    85.         };
    86.      
    87.      
    88.     }
    89. }
    90.  
    91.  
     
  4. CarreraSilvio

    CarreraSilvio

    Joined:
    Jul 21, 2012
    Posts:
    31
    I tried to keep adding torque as the key was held but the problem was that if I held the flipper up before the ball got to it as soon as it touched it it would be propelled upwards, since I was continuosly adding torque, which shouldn't happen on a normal pinball game (the ball should just slide up abit before stopping, depending on the force it had there).

    I think the motor could do the trick...maybe start up a motor if the players keeps holding down the key.