Advertisement
Guest User

Thanks : )

a guest
Dec 17th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PickUpAndThrow : MonoBehaviour
  6. {
  7. private Transform playerPos;
  8. private bool gotPickedUp = false;
  9.  
  10. private bool gotThrown;
  11. private Rigidbody2D rigidbody;
  12. private bool throwLeft;
  13. public float throwSpeed;
  14. // Start is called before the first frame update
  15.  
  16.  
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. if(Input.GetKeyDown(KeyCode.A))
  21. {
  22. throwLeft = true;
  23. }
  24. if(Input.GetKeyDown(KeyCode.D))
  25. {
  26. throwLeft = true;
  27. }
  28. if(gotPickedUp == true && gotThrown == false)
  29. {
  30. transform.position = playerPos.position;
  31. }
  32.  
  33. if(Input.GetKey(KeyCode.E) && gotPickedUp == true)
  34. {
  35. gotThrown = true;
  36. gotPickedUp = false;
  37. if(throwLeft == true)
  38. {
  39. rigidbody.AddForce(Vector3.left * throwSpeed);
  40. }
  41. if(throwLeft == false)
  42. {
  43. rigidbody.AddForce(Vector3.right * throwSpeed);
  44. }
  45. }
  46. }
  47. IEnumerator Start()
  48. {
  49. rigidbody = GetComponent<Rigidbody2D>();
  50. yield return new WaitForSeconds(4);
  51. playerPos = GameObject.Find("Player").GetComponent<Transform>();
  52. }
  53.  
  54. void OnTriggerEnter2D(Collider2D other)
  55. {
  56. if(other.gameObject.CompareTag("Player") && Input.GetKey(KeyCode.Q))
  57. {
  58. gotPickedUp = true;
  59. }
  60. }
  61.  
  62.  
  63. void OnCollisionEnter2D(Collision2D other)
  64. {
  65. gotThrown = false;
  66. }
  67.  
  68.  
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement