- Home /
Problem with collectable item counter and triggers
I am working on a small prototype for a project, this is my first experience with Unity or C#. I have put together what I have so far by reading documentation, following tutorials, and trial and error.
To try and help explain my problem, I have uploaded a web player of it so far, including underneath all my code, and screenshots of the inspector panels. You can also download a zip of the project if that isn't enough (let me know if this doesn't work or I didn't pack it correctly). I apologise if any of my files or code are messy, I've tried to keep them neat enough. All of this can be found here:
http://aiden.lesanto.com/Build1_2/Build1_2.html
[1]: +http://aiden.lesanto.com/Build1_2/Build1_2.html
Basically, I am trying to make the Player collect the pink objects (eggs) and then have a counter (top left) keep track of how many he has collected so far. The objects 'magnetise' towards him when he gets near, using a separate Gameobject with a collision area that is a trigger. When the player is in the trigger the item moves towards him and the object is then destroyed so it looks like he has collected it.
It is the counter that is my problem. If the player moves within the trigger boundary, and stays still until the object moves towards the player, he collects it but it does not add to the value 'Score'. However, if the player moves quickly over where the object is before allowing it to move towards him, it sometimes does add to the Score. It doesn't really add correctly, sometimes it multiplies too many times.
Could anybody take a look at this and help me figure out how to make it behave correctly? I've been stuck for days, but I feel that it might be obvious to someone more experienced. Any advice is appreciated.
Here is some of the code:
coinCollect.cs
public class coinCollect : MonoBehaviour {
// Script Reference
public timerText timerText;
//
public int value;
void OnTriggerEnter2D(Collider2D collidedObject)
{
if (collidedObject.tag == "Player")
{
collidedObject.GetComponent<CoinCounter>().score += value;
timerText.timer += 1;
}
}
}
CoinCounter.cs
public class CoinCounter : MonoBehaviour {
public int score;
public GUIText EggCount;
public int maxEggs = 250;
void Update (){
EggCount.text = "Eggs " + score + " / " + maxEggs;
}
}
MagnetMove.cs
public class MagnetMove : MonoBehaviour {
public Transform target;
public float speed;
void OnTriggerStay2D(Collider2D collidedObject)
{
if (collidedObject.tag == "Player")
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
Destroy(gameObject,0.25f);
}
}
}
timertext.cs
using UnityEngine;
using System.Collections;
public class timerText : MonoBehaviour {
public GUIText timeText;
// The countdown timer
public float timer;
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
timeText.text = "Time " + timer.ToString ("0");
if (timer <= 0){
timeText.text = "";
Time.timeScale = 0;
}
}
}
There is more detail in the link above if this is not enough :)
Generally speaking, it's preferable to post a code snippet rather than the whole project. $$anonymous$$ost folks aren't going to download and investigate it for you, but many will proofread a small block of code to help. Please post only the code which is relevant to the issue.
$$anonymous$$y apologies, the reason I didn't post a snippet at first was because I wasn't sure exactly which part would be causing the problem. I've updated the main post now.
I'm not a FPS guy, so apologies if the questions are not right, but...
Why use 2D triggers, but 3D moveTowards?
Why change the egg position, but then immediately destroy it?
Why destroy the object when egg may have not moved towards player sufficiently?
Why are you not checking the distance between objects and comparing that to some '$$anonymous$$imum distance' for destroying?
Surely it makes more sense to destroy the item and at the same time increment the score?
From what I can see, you haven't posted the code that's responsible for increasing the score.
"I'm not a FPS guy, so apologies if the questions are not right, but..."
Firstly, thank you for answering and Im sorry that a lot of my mistakes might be obvious. Like I said I am a complete beginner. Im not sure what you mean by FPS? To try and answer your comments:
"Why use 2D triggers, but 3D moveTowards?"
Does this make a difference to how the code behaves? Will it cause my code to not work? Or is it simply good practice to keep things consistent? $$anonymous$$y game is 2D, but as far as I have learnt, I thought that sometimes in 2D games using other 2D variables, 'Vector3' is still used?
"Why change the egg position, but then immediately destroy it?" "Why destroy the object when egg may have not moved towards player sufficiently?"
When trying to get this to work, I was aware that this was not the best way to do it. I wanted the egg to move towards the player, then be destroyed when it made contact with him, so it looked like he had collected it. For some reason, I couldn't get it to destroy it any other way (the first way I tried was to add it into coinCollect, in line 15, but it did nothing?), so I made it destroy shortly after moving towards the player, which seemed to simulate the effect I wanted, although falsely, until I could figure out how to do it more correctly.
I'd be thankful if anyone could direct me on how to make it work more properly and what I should change in the code, or where I could put the 'destroy' line?
"Why are you not checking the distance between objects and comparing that to some '$$anonymous$$imum distance' for destroying?"
"Surely it makes more sense to destroy the item and at the same time increment the score?"
To put it simply, the reason I am not doing this is because I don't know how, hence why I posted a question. I will try and look into what you have said here, but any further or more precise direction, such as examples of code, would be helpful.
Honestly, I am quite confused myself, but I thought that this line below did this by adding the value which I assign in the inspector on collision? Or have I simply forgot to add something very obvious?
collidedObject.GetComponent().score += value;
(coinCollect.cs, line 13)
Answer by richyrich · Nov 03, 2014 at 10:38 PM
I feel so heartless. Here's the solution, but please, please, please; go do some tutorials. This is a hack solution, in no way consider this good programming or good design etc. - but its enough to get it working, that you can play with, but then give you inspiration to go do some study - oh and then get rid of loads of code in your project that really should not be there! ;)
coinCollect.cs
using UnityEngine;
using System.Collections;
public class coinCollect : MonoBehaviour {
// Script Reference
public timerText timerText;
public int value;
void OnTriggerEnter2D(Collider2D collidedObject)
{
if (collidedObject.tag == "Player")
{
//collidedObject.GetComponent<CoinCounter>().score += value;
//timerText.timer += 1;
}
}
}
MagnetMove.cs
public class MagnetMove : MonoBehaviour {
public Transform target;
public float speed;
void OnTriggerStay2D(Collider2D collidedObject)
{
if (collidedObject.tag == "Player")
{
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, target.position, step);
if ((Vector2.Distance(transform.position, collidedObject.transform.position) < 0.05f))
{
collidedObject.GetComponent<CoinCounter>().score += 1;
GetComponentInChildren<coinCollect>().timerText.timer += 1;
Destroy(gameObject);
}
}
}
}
//http://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html
PS - Thanks for the project - probably never would have looked into 2D without it ;)
Thanks so much! I am very grateful for this, I will take your advice as well. For now, this solved my problem and let me move onto the next step in my project. (I wanted to upvote, but wouldn't let me as I dont have enough rep) Sorry for late reply.
No worries mate. You can always vote and accept other answers later when you have the $$anonymous$$arma. Good luck with your projects
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to get trigger input on an Xbox controller (Mac) 0 Answers
How to enable gravity on gameobject when interact by player 3 Answers