Alright so I am trying to write a method to grab the object that is closest to the player and then on click it is dropped and the process can be repeated:
Right now this is the method that I am using:
if (Input.GetMouseButtonDown(1) )
{
float radius = 1.50f; // set up the collision radius
Collider[] objectsInRange = Physics.OverlapSphere(transform.position, radius); // array of all objects in collision
bool flag = false; // this flag is for officially grabbing an object so we aren't stuck in the for loop when we dont need to be
// iterate through the array until we find what we want
for(int q=0;q<objectsInRange.GetLength(0) && flag == false;q++){
// is the object interactable?
if(objectsInRange[q].tag == "Interactable"){
GameObject CurItem = (GameObject) objectsInRange[q].gameObject; // let's get the gameobject
isHolding = !isHolding; // change the state of holding
if(isHolding){ // if we are holding something
// want to change the item in our hands to the current item
//objHands = CurItem;
Vector3 rotation = new Vector3(0,0,1);
CurItem.transform.parent = objPlayer.transform;
CurItem.transform.position = objPlayer.transform.position + rotation;
}else{
// remove as the parent and set objHands to something else
CurItem.transform.parent = null;
}
flag = true;
}
}//end for
}
The comments should explain all the logic but if they do not then I will be more than happy to explain what was going on in my head.
In my mind this should simply check the array for all objects in the collision circle. If one of them has the tag “Intractable” then it should be added as a child to the objPlayer and it should be relative to the player without physical effects besides being added to it.
Unfortunately this is not happening right now.
Right now the player can pick up the object and it looks fine but when the character rotates or moves this changes the relative position of the object to the player. Is this because the physical effects are still being applied to the object? If this is the case is there a way to turn it off? Or a way to do this manually with code.
Hopefully I have provided a decent amount of information for you guys to help me but if not just let me know and I will do my best to help out.