How do I make the player carry an object once they have collected it?
Okay so what I'm trying to achieve is this:
When a player walks up to an object and presses the left mouse button, the object will then be carried by the player. Not as in goes into inventory, as in they're physically carrying it.
For example, like a flashlight. Once the player presses a button to pick up the flashlight, it appears in their hands and follows the player.
How would I go about this? Every tutorial I have found talks about putting things in an inventory(like collecting money or token or something similar) which is not what I'm after.
I'm using C# and I would greatly appreciate any help or advice with this :)
(note: I only ask questions as a last resort when I have exhausted all my other options such as tutorials and forums)
Answer by UnityCoach · Apr 06, 2017 at 09:29 AM
Hi, when you handle collectible items, you want to handle several things:
inventory information (the character's picked up the item, can probably dispose it, can maybe store it for later use, etc), this will usually enable some input/UI
actionnable items will usually have a graphics representation (3D model / 2D sprite), along with features/modifiers (script driven). Like a flashlight will have its 3D model in the character's hand and probably enable some flashlight aiming script.
If this is something the character is likely to find early in the game and keep for a long time, I would advise to simply have the object already there, in the character's hand, and simply deactivated. Then you can activate it when you grab it.
If there are many different pickable objects, likely to change over time, then you want to have an anchor (empty game object) in the character's hand, referenced by the ItemPickup component (responsible for item pick up), so you can Instantiate (or simply move) pickup items to that anchor when they're being picked up.
Answer by VeeooshL · Apr 06, 2017 at 09:37 AM
well a good start would be to parent the flashlight to your player:
flashlight.transform.SetParent(Player);
Then, you can set its local position and rotation to look like you are carrying it (you will have to find a good position and rotation by experimenting):
flashlight.transform.localPosition = new Vector3(0, 0, 0) //set this to a good position
flashlight.transform.Rotate(Vector3.forward)
you could then add an animation when your player walks. finally, add functionality like this:
public bool toggle;
void Update(){
if(Input.GetMouseButton(0)){
toggle = !toggle;
if(toggle){
//turn on
}
if(!toggle){
//turn off
}
}
}
that should work :) if you need me to write a complete script for you, that's fine, just tell me.