object pickup

Any advices on how I can have the player(want to be 3rd person but using FS controller for now) pick something up, by pressing a key, from ground when he is close enough.

Any scripts that can help me with this? I just want to see if I can start a simple RPG type game.

Thanks.
Tri

On the game object representing the pickup or otherwise interactive object, add a collider, set it as trigger and add a script handing OnTriggerEnter.

file:///Applications/Unity/Documentation/ScriptReference/Collider.OnTriggerEnter.html

Hi AngryAnt,

Thanks for the link but unfortunately file does not exist. Can you recheck link?

Thanks.

It refers to a file in the Unity.app bundle. I’m guessing you’re trying to access the link from a terminal with no unity installation?

Yes you are right I’m on my pc at work. I’ll try again from my MAC.

ok I created a plane, fs controller and a sphere. Sphere already has sphere collieder. I checked Is Trigger for the sphere and applied OnTriggerEnter script to the sphere. When I test it out the player runs to the sphere stops inside the sphere. I can see that in the Scene view and also in my Game view its all black. Any idea?

I’ve worked with 2 colliders and a placeholder (don’t know if it’s the right and most efficient way buy hey, it works :stuck_out_tongue: )

I’ve added a BoxCollider which handles the trigger and a SphereCollider (in my case, I was throwing a ball) so it can bounce properly.

I also made an empty GameObject as placeholder for my ball. I’ve attached this on my FPS somewhere at hand-level :slight_smile:

If you want the script I will clean it up (it’s quite a mess right now and it involves another script which defines how hard you can throw the object)…

I’ve worked with 2 colliders and a placeholder (don’t know if it’s the right and most efficient way buy hey, it works :stuck_out_tongue: )

I’ve added a BoxCollider which handles the trigger and a SphereCollider (in my case, I was throwing a ball) so it can bounce properly.

I also made an empty GameObject as placeholder for my ball. I’ve attached this on my FPS somewhere at hand-level :slight_smile:

If you want the script I will clean it up (it’s quite a mess right now and it involves another script which defines how hard you can throw the object)…

That would be great to see the script in action. Throwing an object will come in handy in my project.

Thank you.

My project

The ball is upstairs, take it with ‘m’ (or ‘,’). How longer you press the Fire-button, how further you will throw the ball.

Nice project.

Thats pretty much what I need. Can I see the script to how you pick up the ball up? Thanks.

I’ve rewrote the script because it’s wasn’t very reusable… And ow, it’s written in C#!

I’ve attached two scripts. One script handles the pick up, the other script is meant for the object you want the pick up.

I recommend to make an empty GameObject which represents the hand of the player. Make this GameObject a child of the Main Camera of the First Person Controller. And put the script PickUpAndThrow.cs on it.
Place the other script on the object you want to pick up.

Let me know how it went :wink:

PS: I’m still rewriting it so you can’t throw it away yet! Working on it but I’m quite busy right now (need to finalize my project :wink: )

78392–3004–$objectproperties_257.cs (1.04 KB)
78392–3005–$pickupandthrow_819.cs (2.38 KB)

Hi Thomas,

In objectpropertites.cs I wonder if in the 8th line its meant to say two colliders rather than two scripts?

If so how did you set up the hieracy of your “pick me up” object? I cant seem to get the desired results unfortunately.

If anyones having issues getting these into their project remember to rename them so the script names match the “public class” of each script, on lines 13 and 17.

Looking forward to “grasping” this :stuck_out_tongue:
AC

I’d done per your request but wasn’t able to pick up object. Not sure if I missed something.
This may be a stupid question but what key do I use to pick up object?

Ah yes, TWO scripts needs to be TWO colliders :sweat_smile:
One collider for triggering, the other for the correct way of bouncing…

You need to set your own key at Edit >> Project Settings >> Input

I’ve added the project with a simple cube, terrain and the necessary scripts. Have a look :wink: (pick the object with ‘f’)
The only problem I’m having right now is the fact that when I look up, the object doesn’t goes up. The player just throws the object in front of him…

I know I can add a ‘y’-value at the TransformDirection but this shouldn’t be always the same value.

78503–3011–$throwsomething_597.zip (2.8 MB)

Added a script which determines how hard you can throw the object. How longer you hold the mouse button, how further you throw the object…

78586–3016–$throwsomething_804.zip (2.8 MB)

Thomas,

Thank you for the scripts. I’m wondering in the PickUpAndThrow.cs script where you call GrabObject() how would I go about keeping grabbing objects and keep a count of it.

e.g. I have 10 boxes on floor and I would go and pick those up by pressing “f” and keeping a count of what I pick up.

Thanks.

So I suppose you don’t really need to see the objects you picked up? Do you need to throw the objects away?

If so, the first option which pops up in my head is an ArrayList (I don’t know if you’re in the programming stuff?). And you place each object in an ArrayList, read out the size of the ArrayList and you know the amount of objects… (will try it out myself :wink: )

Edit:
Add/change this :slight_smile:

ArrayList objectsPlayer = new ArrayList();

/// How many objects are in the ArrayList
Debug.Log( "ArrayList: " + this.objectsPlayer.Count );

void ThrowObject() {
	GameObject temporary = (GameObject) this.objectsPlayer[ this.objectsPlayer.Count - 1 ];
		
	temporary.rigidbody.isKinematic = false;
	temporary.rigidbody.velocity = transform.TransformDirection(0, 0, this.powerMeter.GetStrength());
		Physics.IgnoreCollision(temporary.collider, transform.root.collider);
	
	temporary.transform.parent = null;
		
	this.objectsPlayer.RemoveAt(this.objectsPlayer.Count - 1);
	}

If you just need to pick up, you can keep a counter.
Something like this:

int count = 0;

void GrabObject() {
  this.count++; // increment this value
}

I won’t need to see the item I’m holding after picking it up but I do want to throw it away.

I’m really a noob to C# scripting so I want to learn the basic first.
My intention is to grasp on how to pickup items. Then for a gate to open when 5 of same items are pickup. Then I want to learn how to drop the item when not needed.

From your supplied scripts I have been absorbing it slowly. Thank you for them.

Once the player picks up the item how do I destroy the item? I think in Javascript its
‘Destroy(GameObject);’? What is C# equivalent?

Thanks.