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 GrabObjects : MonoBehaviour
- {
- public Transform holdSpot;
- public LayerMask pickUpMask;
- public Vector3 direction { get; set; }
- public GameObject itemToHold;
- public bool isgrabbed;
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space) && isgrabbed == false)
- {
- GrabObject();
- }
- }
- void GrabObject()
- {
- if(itemToHold)
- {
- itemToHold.transform.position = transform.position + direction;
- itemToHold.transform.parent = null;
- if (itemToHold.GetComponent<Rigidbody2D>())
- {
- itemToHold.GetComponent<Rigidbody2D>().simulated = true;
- }
- } else
- {
- Collider2D pickupItem = Physics2D.OverlapCircle(transform.position + direction, .4f, pickUpMask);
- if(pickupItem)
- {
- //isgrabbed = true;
- itemToHold = pickupItem.gameObject;
- itemToHold.transform.position = holdSpot.position;
- itemToHold.transform.parent = transform;
- if (itemToHold.GetComponent<Rigidbody2D>())
- {
- itemToHold.GetComponent<Rigidbody2D>().simulated = false;
- }
- } else if (Input.GetKeyDown(KeyCode.Space))
- {
- itemToHold = null;
- isgrabbed = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement