So I have a pickup object script attached to my first person character. I walk up to an object, raycast goes out, if it hits with an object that has the tag “interactable” I hit E and it picks up the object. While carrying it i can right click and throw it. E drops the object. My issue is when im carrying the object in view and look down, it goes through my floor. I understand isKinematic = true is the reason for this, but how do i get it to do that for the object but when it touches any other surface other than objects tagged “interactable” (so like walls and floors) it either drops the object or just simply collides solidly with it. My code is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineOfSight : MonoBehaviour
{
//we assign this script to the first person character
private RaycastHit vision; // this is where ill store the information from the raycast collision
public float rayLength; // length of my raycast
private bool isGrabbed; // the player is carrying the object
private Rigidbody grabbedObject; // ill assign vision to this later as a reference to the objects rigidbody
public float throwForce = 1000; // amount of force for throwing the object
void Start()
{
rayLength = 4.0f; // set the raycast length from the start
isGrabbed = false; // game just started so the player is not carrying an object
}
void Update()
{
// Im using the first person character as my camera position, cast from there in forwards direction, store what I hit into the variable vision, and the raylength is the length of the raycast ray
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out vision, rayLength))
{
if(vision.collider.tag == "interactable")// each interactable object has a tag called interactable
{
if(Input.GetKeyDown (KeyCode.E) && !isGrabbed) // if I hit E and I am not carrying an object
{
grabbedObject = vision.rigidbody; // assign the object we hit with raycasts rigidbody component to the variable grabbedObject
grabbedObject.isKinematic = true; // avoid recognizing collisions
grabbedObject.transform.SetParent(gameObject.transform); // set the grabbed object as a child of the camera
isGrabbed = true; // set the fact that im carrying something now
}
else if(isGrabbed && Input.GetKeyDown (KeyCode.E)) // if i am carrying something and I hit E, drop the object
{
grabbedObject.transform.parent = null; //detach from parent
grabbedObject.isKinematic = false; // turn collision detection on
isGrabbed = false; // set it so im not shown as carrying anything so that I can hit E again and pick it back up
}
if (Input.GetMouseButtonDown(1)) // if i click right mouse button
{
grabbedObject.isKinematic = false; // bring back collision detection
grabbedObject.transform.parent = null; // remove from parent
isGrabbed = false; // set it so im not carrying anything so i can click E and pick up again
grabbedObject.AddForce(Camera.main.transform.forward * throwForce); // using the objects rigidbody, add force to it in forward direction
}
}
}
}
}