Hi, so the thing is, when I am inside pickup range of multiple “logs”, I pick up multiple logs, and not just 1. Since the script is attached to each prefab or “log”, I am not sure how to proceed… I just need a little push in the right direction.
This is my pickup script :
using UnityEngine;
using System.Collections;
public class Pickup : MonoBehaviour
{
private bool canPickUp = false;
private bool hasLog = false;
public GameObject log;
// Use this for initialization
void Start ()
{
log = GameObject.Find ("Log");
}
// Update is called once per frame:P
void Update ()
{
if(!hasLog)
{
if(canPickUp && Input.GetKeyUp(KeyCode.T))
{
PickUp();
}
}
else
{
if(Input.GetKeyUp(KeyCode.T))
{
Drop ();
}
}
}
void OnTriggerExit(Collider other)
{
if(other.collider.tag == "Player")
{
canPickUp = false;
}
}
void OnTriggerEnter (Collider other)
{
if(other.collider.tag == "Player")
{
canPickUp = true;
}
}
void PickUp ()
{
//change the position of the object to hand
gameObject.transform.position = log.transform.position;
gameObject.transform.rotation = log.transform.rotation;
gameObject.transform.parent = log.transform;
gameObject.collider.enabled = false;
gameObject.rigidbody.isKinematic = true;
hasLog = true;
}
void Drop ()
{
gameObject.transform.parent = null;
gameObject.rigidbody.isKinematic = false;
gameObject.collider.enabled = true;
hasLog = false;
}
}