Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PickUpAndThrow : MonoBehaviour
- {
- private Transform playerPos;
- private bool gotPickedUp = false;
- private bool gotThrown;
- private Rigidbody2D rigidbody;
- private bool throwLeft;
- public float throwSpeed;
- // Start is called before the first frame update
- // Update is called once per frame
- void Update()
- {
- if(Input.GetKeyDown(KeyCode.A))
- {
- throwLeft = true;
- }
- if(Input.GetKeyDown(KeyCode.D))
- {
- throwLeft = true;
- }
- if(gotPickedUp == true && gotThrown == false)
- {
- transform.position = playerPos.position;
- }
- if(Input.GetKey(KeyCode.E) && gotPickedUp == true)
- {
- gotThrown = true;
- gotPickedUp = false;
- if(throwLeft == true)
- {
- rigidbody.AddForce(Vector3.left * throwSpeed);
- }
- if(throwLeft == false)
- {
- rigidbody.AddForce(Vector3.right * throwSpeed);
- }
- }
- }
- IEnumerator Start()
- {
- rigidbody = GetComponent<Rigidbody2D>();
- yield return new WaitForSeconds(4);
- playerPos = GameObject.Find("Player").GetComponent<Transform>();
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- if(other.gameObject.CompareTag("Player") && Input.GetKey(KeyCode.Q))
- {
- gotPickedUp = true;
- }
- }
- void OnCollisionEnter2D(Collision2D other)
- {
- gotThrown = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement