22
33public class SpringActivator : MonoBehaviour
44{
5- public SpringJoint2D springJoint ; // Spring Joint on the base
6- public Rigidbody2D ball ; // Ball to be launched
7- public float springForce = 20f ; // Adjust the spring force
5+ [ Header ( "References" ) ]
6+ [ SerializeField ] private SpringJoint2D springJoint ; // Spring Joint on the base
7+ [ SerializeField ] private Rigidbody2D ball ; // Ball to be launched
8+
9+ [ Header ( "Spring Settings" ) ]
10+ [ SerializeField ] private float springForce = 20f ; // Adjustable spring force
11+ [ SerializeField ] private Vector2 forceDirection = Vector2 . right ; // Force direction
12+
13+ [ Header ( "Timing Settings" ) ]
14+ [ SerializeField ] private float springResetDelay = 0.5f ; // Delay before resetting the spring
15+ [ SerializeField ] private float resetDistance = 1f ; // Distance to reset after launch
16+
17+ [ Header ( "Input Settings" ) ]
18+ [ SerializeField ] private KeyCode activationKey = KeyCode . Space ; // Key for activating the spring
819
920 private bool isActivated = false ;
1021
11- void Update ( )
22+ private void Update ( )
1223 {
13- if ( Input . GetKeyDown ( KeyCode . Space ) && ! isActivated )
24+ if ( Input . GetKeyDown ( activationKey ) && ! isActivated )
1425 {
1526 ActivateSpring ( ) ;
1627 }
@@ -20,16 +31,18 @@ private void ActivateSpring()
2031 {
2132 isActivated = true ;
2233
23- // Apply force horizontally to the ball
24- ball . AddForce ( Vector2 . right * springForce , ForceMode2D . Impulse ) ;
34+ // Apply force to the ball
35+ ball . AddForce ( forceDirection * springForce , ForceMode2D . Impulse ) ;
36+ Debug . Log ( "Spring activated!" ) ;
2537
26- // Simulate release after a short delay
27- Invoke ( nameof ( ResetSpring ) , 0.5f ) ;
38+ // Reset spring after a delay
39+ Invoke ( nameof ( ResetSpring ) , springResetDelay ) ;
2840 }
2941
3042 private void ResetSpring ( )
3143 {
32- springJoint . distance = 1f ; // Reset distance (if applicable)
44+ springJoint . distance = resetDistance ; // Reset the spring distance
3345 isActivated = false ;
46+ Debug . Log ( "Spring reset!" ) ;
3447 }
3548}
0 commit comments