"First of all my entire project is here for reference: https://github.com/Some-T/FirstPersonController-CSharp
Secondly I am next trying to implement a picking up the object feature so I have been outlining the steps I need to do this and was wondering if I am correct in my thinking here, is this the best way to do it?
- Use Unity - Scripting API: Physics.OverlapSphere to detect the object in range
- Initiate picking up of the object in first person mode, I take it here I would reduce the object detected in range gravity and mass to make it be able to hold position relative to the player
- When I let go of a certain button the gravity and mass is restored and its position goes back to free roam via the dynamics of its mass and gravity.
Elsewhere I see people use raycasting to pick up objects in first person view mode?
Am I over complicating it here or is this the best way which I listed above to pick up objects in game in first person view mode? Any advice and input here would be much appreciated."
What this means:
Initially I had an issue picking up an object. Moving forwards I am thinking raycasts is actually the best way to do it? but I am not sure??? as you can see from the now long code history and attempts via the github repo (please tell me if that is not helpful then I can detail each iteration of the code, I think about 100 in total of stuff I have tried) One of the key issues I am having at the moment, if I have a object held then crouch the object will always change scale because the y axis of the player changes scale so this is why I am thinking raycasts? But not so sure as not too great with these, my other experiments have not had success with the held object following the player well at all, again there are a lot of different versions of code here, I am happy to put them all on here, it might take me a while though, its honestly easier if you look at the github repo but if mods are not happy with it that I am more than willing to post all code iterations here.
From the next post, the response we have:
"Use Unity - Scripting API: Physics.OverlapSphere to detect the object in range
This can work but I think it’s more common to just attach a trigger collider to either the player or the pickup-able object and rely on OnTriggerEnter and OnTriggerExit to tell when an object is eligible for pickup.
SomeT said: ↑
- Initiate picking up of the object in first person mode, I take it here I would reduce the object detected in range gravity and mass to make it be able to hold position relative to the player
Typically you would just set the object to kinematic and/or disable its collider and start controlling its position from script directly, or just parent the object to the player.
SomeT said: ↑
Am I over complicating it here or is this the best way which I listed above to pick up objects in game in first person view mode?
There isn’t really a “best way”. It depends on how you want your game to work.
Do you want the player to be able to pick something up based on the simple distance to the object, or should they be looking at the object to be able to pick it up?
While holding the object, should it just perfectly sit at a certain position relative to the player as the player moves around? Or should it still be affected by physics? Should it collide with things? Or Just pass right through things? If it collides should it also be affected by the collision? Should it be able to be knocked out of the player’s grip?
The answers to all these questions should inform how you choose to implement this."
I believe I have tried similar things to this, however, I am not sure if a follow up was due, I answered the questions as quickly as possible and waited patiently, around a year for a response but I don’t think that was necessary upon reflection and I apologise. But a year on I am still struggling, I answered the questions above in a previous post so rather than repeating again I will go onto to break down where I am at right now as still trying to find the best way to achieve this:
This brings me to the latest aspects, more questions :
-
Subject line is already chosen, not sure if I can change this?
-
A lot of the stuff I got in google bought me to very old unity answers, often working around the concept of not changing the child object when crouching
-
Not sure what is meant by ballpark to be honest, I struggled on this question. I tried my best to understand the rest though, so my latest code involves the ducking mechanism mainly:
void PickupObject(GameObject pickObj)
{
if (pickObj.GetComponent<Rigidbody>())
{
Rigidbody objRig = pickObj.GetComponent<Rigidbody>();
objRig.useGravity = false;
objRig.drag = 10;
objRig.transform.parent = holdParent;
heldObj = pickObj;
}
}
void MoveObject()
{
if (Vector3.Distance(heldObj.transform.position, holdParent.position) > 0.1f)
{
Vector3 moveDirection = (holdParent.position - heldObj.transform.position);
heldObj.GetComponent<Rigidbody>().AddForce(moveDirection * moveForce);
}
}
[code=CSharp] void DropObject()
{
Rigidbody heldRig = heldObj.GetComponent<Rigidbody>();
heldRig.useGravity = true;
heldRig.drag = 1;
heldObj.transform.parent = null;
heldObj = null;
}
but the above code when I crouch with this code
private bool IsDucking
{
get
{
return isDucking;
}
set
{
isDucking = value;
if (isDucking)
{
mainPlayer.transform.localScale = new Vector3(1.0f, 0.5f, 1.0f);
jumpPower = 0;
}
else
{
mainPlayer.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
jumpPower = 5;
}
}
}
where it says 0.5 its also changing the y dimension of the scale transform (local) of the child object, thus the held object, I need it to stop doing that, where I started to think about raycasts. I think the overlap sphere thing is a dead area, the questions I answered relating to that they did’nt lead me anywhere in my year long investigation.
Additionally if this response is not acceptable, I can go really in depth if need be and write up some in depth technical documentation detailing every code example so far. I feel really bad, and I do not intend to upset the mods here, I am just trying to find the best way to pick up objects in a first person game overall, and I believe raycasts is the best way to do this? If someone could help me on that question alone I think it would be the most helpful thing going forwards. I am also autistic so sorry if this is getting messy, I have learning difficulties and am tripping over myself a bit here, but I thought coming back to this rather than making a new topic entirely was a better idea. Anyway that leaves the additional remaining questions apologies if some of it repeats but really wanted to get this all right:
- What you think it should be doing
Picking up a game object, but the game object never changes size, I have tried to even pre define the objects size but this still does not work, the parent object always takes precedent on size.
- What it actually is doing
Changing the size of the held object, the child object.
- What things you have tried in order to fix it
Many things, I advised some of them briefly above, but theres about 100 commits or so in the space of a year, its going to take me a while to compile them in more detail if need be.
- Any other interesting tidbits you think might be relevant
None. Link to code though again is: https://github.com/Some-T/FirstPersonController-CSharp/blob/master/Assets/Scripts
Overall what is the best method to pick up objects in a 3D environment on a first person controller?
I am not asking for code here, just that when I follow through on tutorials there are so many ways of doing it, and I hit multiple dead ends especially on this crouch and unintentional resizing mechanic as mentioned above, things don’t always follow through like above, that’s all I want to know really overall.
Lastly a few more questions to answer:
“Are you asking for definitive answers to all 27 varieties of things you listed above??”
Not entirely sure which post you mean, but basically at the moment I am looking at raycasts, as most tutorials either use raycasts or the current method I have implemented.
“In one hour of intense testing you could answer 100% of your questions definitively, trying all the combinations that you list in placeholder disembodied camera picks up rigidbody cube and tosses it context.” I think through testing I have got to this point, but honestly I am not sure what you mean by trying all the combinations that you list in placeholder disembodied camera picks up rigidbody cube and tosses it context exactly?
“If that’s not what you’re asking, well I don’t know what it is, so let’s begin at the begin, shall we?”
Yes definitely 100%, as above, at least my best attempt anyway, honestly I regret responding to this now, I could have easily self-reflected on this myself then gone off and tried raycasts then add it to my tried and failed / possibly successful attempts? I might just do that anyway to be honest.