- Home /
Adds too many points to the score?
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 one whenever it destroys an enemy and/or only one animation plays?
Thanks, I this 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;
}
}
}
Without seeing your code it is impossible to tell what you are doing wrong.
Please post it, properly formatted in a code block.
A few more details might make it easier for somebody to help you. For instance, are you using 2D or 3D? OnCollision or OnTrigger?