- Home /
How to make a character collect items?
I've basically never used Unity so i have no clue how to do this.
I want my character to collect the 'gems' and for each gem that is collected the player gets a point. Simple as that.
All i have set up just now is my terrain with the character walking about and the gems on the terrain.
would really appreciate some help as it's for my coursework and i'm really stuck and how to do this :(*
Answer by aaronpaulina · Nov 27, 2012 at 12:06 PM
Save this as "SmartCollider.cs" and drag it onto your player object..
`using UnityEngine; using System.Collections;
public class SmartCollide : MonoBehaviour {
//GameObject you might collide with
public Collider CollideWith;
//if you'd like to call a function on yourself when you collide with CollideWith
public string MessageToSelf;
//calls a function on the collider object
public string MessageToCollider;
//If you want to call a function on yourself when they collide.
public bool SendToSelf = false;
public string OnCollisionEnterMessage, OnCollisionExitMessage, OnCollisionStayMessage,
OnTriggerEnterMessage, OnTriggerExitMessage, OnTriggerStayMessage;
void OnCollisionEnter(Collision collision)
{
if (collision.collider == CollideWith)
{
CollideWith.SendMessage(OnCollisionEnterMessage, SendMessageOptions.DontRequireReceiver);
if (SendToSelf == true)
{
this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
}
}
}
void OnCollisionExit(Collision collision)
{
if (collision.collider == CollideWith)
{
CollideWith.SendMessage(OnCollisionExitMessage, SendMessageOptions.DontRequireReceiver);
if (SendToSelf == true)
{
this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
}
}
}
void OnTriggerEnter(Collider collision)
{
if (collision.collider == CollideWith)
{
CollideWith.SendMessage(OnTriggerEnterMessage, SendMessageOptions.DontRequireReceiver);
if (SendToSelf == true)
{
this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
}
}
}
void OnTriggerExit(Collider collision)
{
if (collision.collider == CollideWith)
{
CollideWith.SendMessage(OnTriggerExitMessage, SendMessageOptions.DontRequireReceiver);
if (SendToSelf == true)
{
this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
}
}
}
void OnTriggerStay(Collider collision)
{
if (collision.collider == CollideWith)
{
CollideWith.SendMessage(OnTriggerStayMessage, SendMessageOptions.DontRequireReceiver);
if (SendToSelf == true)
{
this.SendMessage(MessageToSelf, SendMessageOptions.DontRequireReceiver);
}
}
}
}`
Now just create a function on your Gem script `public void AddGem() { OurGemsVariable += 1; }`
drag your gem object onto the Smart collide scipt where it says "CollideWith" and under the OnCollisionEnterMessage type in " AddGem "
now when your player runs into the gem it should add +1 to your gems :)
Thanks a lot guys! i'll give that code a wee try then :)
Answer by Alig · Mar 22, 2013 at 06:14 AM
Can you help please with the same the but, I have a plane and I want to collect a gem through a spiral
thank so much................
Answer by RadicalGuy · Mar 12, 2020 at 07:24 PM
You can do this easy!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collectable : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
//add points
//add ability
//add any powerup here
Destroy(this.gameObject);
}
}
}
make sure to add a box collider with trigger enabled
and make sure your player is tagged Player
Your answer
Follow this Question
Related Questions
Collestion of objects 3 Answers
Problem About Gaining Points 1 Answer
Non stopping point - counter 1 Answer
Point system, something wrong 1 Answer
score not updating 1 Answer