How would I go about making berries (C#)
So I'm making a survival game, and working on the part where the player collides with a block to restore hunger, berries, basically.
So what I'm essentially wondering is how would I associate a block with the FPSController, basically giving the player a body, and then coding so that if the player's body collides with the berry, the hunger of the player increases.
I already have all the vitals done.
Answer by oStaiko · Feb 15, 2017 at 06:58 AM
Put a collision box on the player and berries
Write a script that detects collisions using OnCollisionEnter(Collider other)
Test for tag of other to see if its a berry
If berry, do stuff, if not, do nothing.
Okay, so I added a box collider to my FPSController's components and also to the berries. I have a playervitals script that basically controls everything having to do with the player's vitals, including hunger. So, what I'm wondering, is how would I access the colliders inside of my playervitals script so that way I can access the hunger vital and switch it based on if the player collides with the berry boxcollider?
That would be the most desire able way to do this.
Write a script that detects collisions using OnCollisionEnter(Collider other), step 2.
void OnTriggerEnter (collider other)
{
if (other.CompareTag('Berry"))
{
//set player stuff here
Destroy(other);
}
}
Set berry collider to be a trigger, and tag the berry "Berry". OnTriggerEnter is called whenever the attached gameObjects collider enters any other collider marked as isTrigger.