Holding object not working. Raycast, and first person.

You probably heard this before cause I read a number of similar Qs, none providing an answer though, but I’m trying to achieve an exact effect of holding and object as in the Amnesia game. xD

I suck at raycasting bad I’ve read dozens of questions and watched dozens of videos, including the official one but I just don’t understand it quite yet. I need a for dummies guide on raycasting. x’D

Anyway here’s the code I have now which isn’t working. I’m trying to parent it to hold it and unparent to let go, which works only when its not being affected by outside force. And I want it non-kinematic so it doesn’t pass through objects but if I make it like that once its pushed out of the raycast I can’t seem to unparent it anymore. There’s tons of stuff wrong with my code I bet so if anyone has a better way of doing it or a tutorial for exactly that I’ll gladly read or watch that, I couldn’t find any tutorial for that.

var pickupDistance : int = 10;
var holding : boolean = false;

function Update()
{
	//RAYCASTING
	var hit : RaycastHit; 
	var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width / 2, Screen.height / 2, 0));
	
	if(Physics.Raycast (ray, hit, pickupDistance))
	{
		//Check if the object is tagged Pickupable and click with mouse
		if(Input.GetMouseButtonDown(0) && hit.collider.tag == "Pickupable")
		{
			Debug.Log("Clicked on " + hit.transform.name);
			//Make the tagged object child of the camera and follow it, without being kinematic
			hit.transform.parent = gameObject.transform;
			hit.rigidbody.useGravity = false;
			holding = true;
		}
		//If mouse button up, make the child object have gravity, unparent and let go
		else if(Input.GetMouseButtonUp(0) && holding)
		{
			hit.rigidbody.useGravity = true;
			Drop();
		}
		//Do the same thing cause the child was affected by outside force and moved from local X 0 and Y 0 coordinates, but in a different way because the child is no longer hit by raycast
		else if(hit.transform.localPosition.x > 0 || hit.transform.localPosition.y > 0 && holding)
		{
			var rigidbodyGravity : Rigidbody;
			rigidbodyGravity = gameObject.GetComponentInChildren(Rigidbody);
			rigidbodyGravity.useGravity = true;
			Drop();
		}
	}
	//if holding is true snap the object to camera local X and Y and the transforms Z
	if(holding)
	{
		hit.transform.localPosition = Vector3(0, 0,hit.transform.position.z);
	}
}
//Drop all children
function Drop()
{
	transform.DetachChildren();
	holding = false;
}

I would do this without making the object a child of the player. I don’t think that’s necessary and it could over complicate things potentially with no real benefits that I can think of.

I think what you need to do in short is cast a Raycast, if it hits the right kind of object you store that object in the Transform position of one of the player’s child objects (this object simulates their hand). So you could use any empty gameObject as the child object (or a bone for their hand if you already have the rig) and put the picked up object at that transform position rather than setting the object to be part of the player.

Then you could update the position of the object relative to the transform in FixedUpdate() to allow it to move around with physics. Getting all that set up realistically would require a bit of nice code but shouldn’t be to overwhelming since Unity has all the physics stuff set up in PhysX I beleive?

So you could allow Unity’s normal rigid body physics to affect the object and just make sure it’s always a certain distance from the child transform position. so it doesn’t just drop on the floor unless you throw it or whatever.

This would probably be easier to do if the player was kinematic and the objects were not, that way it’s just the object flailing around and the player movement is mainly controlled by code perhaps gravity. That’s just my guess though!

Then when you throw the object you could apply force to it and remove the condition that makes it be a max distance from the hand child object.

That’s the basics of how I would try to set it up not sure how much it will help you specifically since I never actually tried this is in any of my games.

So your problem isn’t how to pick up items right it’s how to make you hold them realistically? Hopefully my answer isn’t way off topic haha.