SImple but IMPOSSIBLE Enemies keep dying way ahead of time !
my enemies keep dying way ahead of time ! this is because I have a left screen collider wall that kills the enemies when they touch/leave the screen , but I need the repeat background so that the background scrolls and I need to kill the enemies when they touch the screen walls
let me explain- I have a 2d background that repeats , and since I have colliders in the scene on the background walls It kills the enemies at wrong times for example when some enemies reach the middle ,and beginning etc of the screen they die because the repeat background is repeating
so what do I do here? I need both
the screen colliders and I need the repeat scrolling background , this is a tough situation - 2d game
so again please understand the problem here- I need both these things but clearly this is a very tricky situation -
please assist me thank you very much
here is the background scroll script ,
using UnityEngine;
using System.Collections;
public class bgscroll : MonoBehaviour {
public float speed = 0.2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector2 offset = new Vector2 (Time.time * speed, 0);
GetComponent<Renderer>().material.mainTextureOffset = offset;
}
}
--------------------------------SCRIPT 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletDestroyScript : MonoBehaviour {
void OnEnable()
{
Invoke ("Destroy", 7.6f);
}
void Destroy()
{
gameObject.SetActive(false);
}
void OnDisable()
{
CancelInvoke();
}
}
===============SCRIPT 3
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class mandedd : MonoBehaviour {
public AudioClip pop1;
public AudioClip boing;
AudioSource audio;
Animator anim;
void Start()
{
audio = GetComponent<AudioSource>();
anim = GetComponent<Animator> ();
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player" || other.gameObject.tag == "Wall" )
{
anim.Play("mansded");
anim.SetTrigger("Die");
GetComponent<AudioSource> ().Play ();
audio.PlayOneShot(pop1, 0.7F);
DeathWatch.ReportDeath ();
}
else if (other.gameObject.tag == "cat" || other.gameObject.tag == "cats" )
{
anim.Play("manded");
anim.SetTrigger("Die");
GetComponent<AudioSource> ().Play ();
audio.PlayOneShot(boing, 0.7F);
DeathWatch.ReportDeath ();
}
}
}
You got to post the code that kills the enemies othwerwise nobody will be able to help you. Also some info about the colliders, are they triggers?
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class mandedd : $$anonymous$$onoBehaviour {
public AudioClip pop1;
public AudioClip boing;
AudioSource audio;
Animator anim;
void Start()
{
audio = GetComponent<AudioSource>();
anim = GetComponent<Animator> ();
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Player" || other.gameObject.tag == "Wall" )
{
anim.Play("mansded");
anim.SetTrigger("Die");
GetComponent<AudioSource> ().Play ();
audio.PlayOneShot(pop1, 0.7F);
DeathWatch.ReportDeath ();
}
else if (other.gameObject.tag == "cat" || other.gameObject.tag == "cats" )
{
anim.Play("manded");
anim.SetTrigger("Die");
GetComponent<AudioSource> ().Play ();
audio.PlayOneShot(boing, 0.7F);
DeathWatch.ReportDeath ();
}
}
}
and here is the other enemy kill script on the enemy
there are 2 scripts as u see one is kill after certain amount of time after they are spawned and hit wall
don't get confused the enemy is named "bullet"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletDestroyScript : $$anonymous$$onoBehaviour {
void OnEnable()
{
Invoke ("Destroy", 7.6f);
}
void Destroy()
{
gameObject.SetActive(false);
}
void OnDisable()
{
CancelInvoke();
}
}
is the background wall tagged as wall? if yes that s most probably causing the problem. The code seems fine.
What about putting the colliders in a separate game object that does not move with the background?
Your answer
Follow this Question
Related Questions
Gameobject Gravity 2D Script 0 Answers
Help with making an object dodge other objects? 0 Answers
Player should turn in the direction the player is running. (2D Game) 0 Answers
Correct Call function On Gui ? 0 Answers
IsTouchingLayers dont work : 0 Answers