- Home /
How to add the right amount of points when my player destroys a gameobject?
Can anybody help me with my scoring? I have it so that the player gets a point whenever it destroys an enemy but what happens is that sometimes it adds too many. It sometimes works as it should and adds 1 point to the score but it occasionally adds 3,4 or sometimes even 10. I don't know if it's also related but I tried making it so that an animation plays once when it hits the front check but the animation always plays twice.
How can I make it that it only adds 1 point whenever it destroys an enemy and/or only one animation plays?
Thanks, I think this is all basic but I've tried researching this all over the net but nothing works.
void Awake()
{
frontCheck = transform.Find("frontCheck").transform;
beak = transform.Find("beak").transform;
}
void Start ()
{
animator = GetComponent<Animator>();
}
void Update ()
{
walking = true;
}
void FixedUpdate ()
{
GroundCheck ();
if (grounded == false || dragged == true)
{
float h = Input.GetAxis ("Mouse X");
if (h > 0 && !facingRight)
Flip ();
else if (h < 0 && facingRight)
Flip ();
}
if (grounded == true)
{
if (walking == true) {
walker ();
}
Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);
foreach (Collider2D c in frontHits) {
if (c.tag == "DirectionTrigger") {
Flip ();
break;
}
if (c.tag == "Enemy") {
animator.SetTrigger ("dash");
walking = false;
break;
}
}
Collider2D[] beakhits = Physics2D.OverlapPointAll (beak.position);
foreach (Collider2D b in beakhits) {
if (b.tag == "Enemy")
{
if (b.gameObject.GetComponent<Waspbot> ().BodyHit == true)
{
b.gameObject.GetComponent<Waspbot> ().HitReceived (transform.position);
CollateralDamage ();
}
}
}
}
if(HP <= 0 && !dead)
{
Death ();
StartCoroutine(delayAnim());
}
}
public void walker()
{
rigidbody2D.velocity = new Vector3 (transform.localScale.x * WalkSpeed, rigidbody2D.velocity.y, transform.localScale.y * WalkSpeed);
}
void GroundCheck()
{
Debug.Log ("grounded");
Debug.DrawLine (sightStart.position, sightEnd.position, Color.blue);
grounded = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("HighGround"));
}
public void Flip()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the x component of localScale by -1.
Vector3 beakbotScale = transform.localScale;
beakbotScale.x *= -1;
transform.localScale = beakbotScale;
}
public void Hurt()
{
HP--;
Debug.Log (HP);
}
void Death()
{
StartCoroutine(Explosion());
}
public IEnumerator delayAnim()
{
yield return new WaitForSeconds(0.01f);
animator.SetTrigger ("explosion");
}
public IEnumerator Explosion()
{
if (HP <= 0)
{
yield return new WaitForSeconds(0.6f);
Destroy (gameObject);
GameObject go = GameObject.FindGameObjectWithTag("Botton");
Bottons sb = (Bottons)go.GetComponent(typeof(Bottons));
sb.bbotcount -= 1;
}
}
void OnMouseDown()
{
dragged = true;
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) , offset;
transform.position = curPosition;
}
void OnMouseUp()
{
dragged = false;
}
public void CollateralDamage ()
{
HP -= 1;
}
void OnTriggerEnter2D (Collider2D bbcol){
// If it hits an enemy...
if (bbcol.tag == "Enemy") {
if (bbcol.gameObject.GetComponent<Waspbot> ().BodyHit == true)
{
bbcol.gameObject.GetComponent<Waspbot> ().RemoveHp ();
HP -= 2;
}
}
}
Where in this code is the score counting happening?
You could give the enemy scrips a bool that gets switched to true when you want to give the points from it and use the same boolean in an if condition to prevent doing it again.
oh sorry I thought it was solely on my player, the scoring is in my enemy. Here's the code:
void FixedUpdate ()
{
if(gameObject.transform.position.x >= 0 && transform.localScale.x >=0)
{
// ... Flip the enemy and stop checking the other colliders.
Flip ();
}
if (walking == true)
{
walker();
}
if (HP <= 0 && !dead)
{
Death ();
StartCoroutine(delayAnim());
walking = false;
}
Debug.Log (HP);
}
public void Flip()
{
Vector3 waspbotScale = transform.localScale;
waspbotScale.x *= -1;
transform.localScale = waspbotScale;
}
public void walker()
{
rigidbody2D.velocity = new Vector2(transform.localScale.x * WalkSpeed, rigidbody2D.velocity.y);
}
void Death()
{
// Destroy the rocket.
StartCoroutine(Explosion());
}
public IEnumerator delayAnim()
{
yield return new WaitForSeconds(0.01f);
animator.SetTrigger ("explosion");
}
public IEnumerator Explosion()
{
if (HP <= 0)
{
yield return new WaitForSeconds(0.6f);
Destroy (gameObject);
if (HitCore == false)
{
Scoring();
// Create a vector that is just above the enemy.
Vector3 scorePos;
scorePos = transform.position;
scorePos.y += 1.5f;
// Instantiate the 100 points prefab at this point.
Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
}
}
}
void OnTriggerEnter2D (Collider2D col)
{
// If it hits an enemy...
// if (col.tag == "swatter") {
// if (transform.position.x < 0f) {
// transform.Translate(Vector2.right * -15 * Time.deltaTime);
// } else if (transform.position.x > 0f) {
// transform.Translate(Vector2.right * 15 * Time.deltaTime);
// }
// }
}
public void bumpLeft()
{
transform.Translate (Vector2.right * -25 * Time.deltaTime);
}
public void bumpRight()
{
transform.Translate (Vector2.right * 25 * Time.deltaTime);
}
public void HitReceived(Vector3 playerPos){
RemoveHp(); //remove those 2hp
BumpHandler(playerPos);
}
public void RemoveHp(){
HP -= 2;
}
private void BumpHandler(Vector3 player){
if (player.x > transform.position.x){
bumpLeft();
}
else{
bumpRight();
}
}
public void CoreHit()
{
GameObject go = GameObject.FindGameObjectWithTag ("Core");
//Grab the script
CoreScript sb = (CoreScript)go.GetComponent (typeof(CoreScript));
//Call the function
sb.HP -= 1;
HitCore = true;
}
public void Scoring()
{
score.score += 1;
scoreShadow.score += 1;
}
Sorry but I don't exactly know where it is.. I thought that maybe its related to some area of the script but here are the functions directly related to the scoring
void FixedUpdate ()
{
if(gameObject.transform.position.x >= 0 && transform.localScale.x >=0)
{
Flip ();
}
if (walking == true)
{
walker();
}
if (HP <= 0 && !dead)
{
Death ();
StartCoroutine(delayAnim());
walking = false;
}
Debug.Log (HP);
}
void Death()
{
StartCoroutine(Explosion());
}
public IEnumerator delayAnim()
{
yield return new WaitForSeconds(0.01f);
animator.SetTrigger ("explosion");
}
public IEnumerator Explosion()
{
if (HP <= 0)
{
yield return new WaitForSeconds(0.6f);
Destroy (gameObject);
if (HitCore == false)
{
Scoring();
// Create a vector that is just above the enemy.
Vector3 scorePos;
scorePos = transform.position;
scorePos.y += 1.5f;
// Instantiate the 100 points prefab at this point.
Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
}
}
}
public void HitReceived(Vector3 playerPos)
{
RemoveHp(); //remove those 2hp
BumpHandler(playerPos);
}
public void RemoveHp()
{
HP -= 2;
}
public void Scoring()
{
score.score += 1;
scoreShadow.score += 1;
}
Sorry I can't isolate it more but this is my first game and I'm having trouble getting it done.
Your answer
Follow this Question
Related Questions
Whats wrong with my score script 1 Answer
Mission Objectives 1 Answer
Collide with projection 1 Answer
Trouble Descaling 2D object 1 Answer
Collecting Array items in order 1 Answer