What the code below does is simply attach `pickUpObject` to the parent transform (in this case, a first person controller) on a press of control and if the ray cast intersects with the box within 100 units. The `pickUpObject`is assigned in the Inspector and that object has a box collider on it. However, the Debug.Log() never triggers.
using UnityEngine;
using System.Collections;
public class PressedLogic : MonoBehaviour
{
public Transform pickUpObject;
private bool pickedUp = false;
// Update is called once per frame
void Update()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Input.GetKeyDown(KeyCode.LeftControl) && Physics.Raycast(transform.position, fwd, 100))
{
if (!pickedUp) //Are we holding something?
{
Debug.Log("Ray hit, and we aren't holding anything");
pickUpObject.position = transform.TransformPoint(Vector3.forward);
pickUpObject.parent = transform;
pickedUp = true;
}
else if (pickedUp)
{
pickedUp = false;
pickUpObject.parent = null; //No parent, so it sets it back to root hierachy
}
}
}
}
Why not use a collision checker between the object you want to pick up and your character? Ray casting seems like more work.
– SirGiveMaybe that would be a good idea...
– anon11421030