For picking-up objects, I chose to make the picked-up object float in the middle of the screen in front of the camera following a certain point.
raycastPos = mainCamera.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit hit;
if (Physics.Raycast(raycastPos, mainCamera.transform.forward, out hit, maxDistance, 1 << interactableLayerIndex))
{
lookObject = hit.collider.transform.gameObject;
reticle.color = new Color(1, 0, 0, 0.75f);
}
else
{
lookObject = null;
reticle.color = new Color(1, 1, 1, 0.75f);
}
if (PickingUp)
{
if (currentlyPickedUpObject == null)
{
if (lookObject != null)
{
if (hit.distance >= minDistance)
{
PickUpObject();
}
}
}
}
public void PickUpObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
// currPicked = true;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
}
I'm facing an issue with this, Whenever the player is standing over a pickable object and picks it up (while standing on it), the player flies on top of the object like this:
Here is another image from the Scene window : You can see clearly here what's happening when the player picks up an object underneath him. How can I prevent that from happening?
CodePudding user response:
Put the picked up object in a new layer. Then disable any physics interaction between player and said layer. You will then observe that the picked up object and the player may intersect. To fix this, you can project the object outside of your player collider using a few more ray casts. Given that the object's collider is convex, calculations should be fairly simple.