I am creating a game where my character can pick up and drop items. However, after picking up the item and dropping it the character can not pick it up again unless the rotation of the object stays the same (the item stays facing upwards like it’s instantiated). After researching I found out that rays don’t always work if an object is rotated. Here is my script for the pickup system:
public class PickUpItem : MonoBehaviour
{
public ItemScript script;
public Rigidbody rb;
public MeshCollider coll;
public Transform player, fpCam, container;
public float pickUpRange;
public float dropForwardForce, dropUpwardForce;
private bool playerexists;
public GameObject item = null;
private Ray interactionRay;
public bool equipped;
public static bool slotFull;
private void Update()
{
if(InteractRayCast() != null && !slotFull) {
item = InteractRayCast();
setItem(item);
Debug.Log("Item found!");
}
if(InteractRayCast() == null && !slotFull) {
item = null;
script = null;
coll = null;
rb = null;
}
Vector3 distanceToPlayer = player.position - transform.position;
if(script != null) {
Debug.Log("Waiting for Key");
if(!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) pickUpItem();
if(equipped && Input.GetKeyDown(KeyCode.Q)) dropItem();
}
}
private void pickUpItem()
{
equipped = true;
slotFull = true;
item.transform.SetParent(container);
item.transform.localPosition = Vector3.zero;
item.transform.localRotation = Quaternion.Euler(Vector3.zero);
rb.isKinematic = true;
coll.isTrigger = true;
script.enabled = true;
Debug.Log("Item Picked Up");
}
private void dropItem()
{
equipped = false;
slotFull = false;
item.transform.SetParent(null);
rb.isKinematic = false;
coll.isTrigger = false;
rb.velocity = player.GetComponent<Rigidbody>().velocity;
rb.AddForce(fpCam.forward * dropForwardForce, ForceMode.Impulse);
rb.AddForce(fpCam.up * dropUpwardForce, ForceMode.Impulse);
float random = Random.Range(-1f, 1f);
rb.AddTorque(new Vector3(random, random, random) * 10);
script.enabled = false;
Debug.Log("Item Dropped");
script = null;
coll = null;
rb = null;
item = null;
}
void setItem(GameObject obj)
{
script = obj.GetComponent<ItemScript>();
rb = obj.GetComponent<Rigidbody>();
coll = obj.GetComponent<MeshCollider>();
}
GameObject InteractRayCast()
{
Vector3 playerPos = player.position;
Vector3 forwardDir = player.forward;
interactionRay = new Ray(playerPos, forwardDir.normalized);
RaycastHit interactionRayHit;
float interactionRayLength = pickUpRange;
Vector3 interactionRayEndpoint = forwardDir * interactionRayLength;
// Debug.DrawLine(playerPos, interactionRayEndpoint);
bool hitFound = Physics.Raycast(interactionRay, out interactionRayHit, interactionRayLength);
if(hitFound)
{
GameObject hitObject = interactionRayHit.transform.gameObject;
string hitFeedback = hitObject.name;
Debug.Log(hitFeedback);
return hitObject;
}
else
{
return null;
}
}
}
How can I check what game object my character is looking at if rays do not work with rotating objects?