Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Plunger : MonoBehaviour {
  6.  
  7. SpringJoint spring;
  8. Rigidbody rb;
  9.  
  10. [SerializeField] float maxPosition = 3F;
  11. [SerializeField] float descSpeed=1;
  12.  
  13. private bool downPressedDown, downPressed, downPressedUp;
  14.  
  15. void Start () {
  16. spring = GetComponent<SpringJoint>();
  17. rb = GetComponent<Rigidbody>();
  18. downPressedDown = downPressed = downPressedUp = false;
  19. }
  20.  
  21. void Update() {
  22. if(Input.GetKeyDown("space")){
  23. downPressedDown=true;
  24. downPressed = false;
  25. downPressedUp = false;
  26. }
  27. else if(Input.GetKey("space")){
  28. downPressedDown=false;
  29. downPressed = true;
  30. downPressedUp = false;
  31. }else if(Input.GetKeyUp("space")){
  32. downPressedDown=false;
  33. downPressed = false;
  34. downPressedUp = true;
  35. }else{
  36. downPressedDown=false;
  37. downPressed = false;
  38. downPressedUp = false;
  39. }
  40. }
  41.  
  42. void FixedUpdate()
  43. {
  44. if(downPressedDown){
  45. spring.maxDistance=maxPosition;
  46. }
  47.  
  48. if(downPressed){
  49. rb.AddForce(0,0,-100,ForceMode.Acceleration);
  50. }
  51.  
  52. if(downPressedUp){
  53. spring.maxDistance=0;
  54. }
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement