Hey everyone, im pretty new to all this but im having a weird bug where my enemy bullets show up the whole time in scene view but wont show up in my game view UNTIL my player is right over top of the enemie’s position. Ive gone through and checked all my order in layer and layer settings to make sure its all correct so nothing is being hidden under another part but i just cant figure this out. Also i cant get the bullets to fire the other direction when the enemy turns around. Any help on this is greatly apppreciated thanks in advance!
Here’s my enemy and bullets scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[Header ("Stats")]
public float walkSpeed;
public float stoppingDistance;
public float nearDistance;
public float startTimeBtwShots;
public float shootSpeed;
private float timeBtwShots;
public Transform firePoint2;
private Vector2 direction;
[Header ("References")]
private Transform player;
public int health = 100;
public GameObject deathEffect;
public GameObject bullet;
public void TakeDamage (int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Start ()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
timeBtwShots = startTimeBtwShots;
}
void Update ()
{
//Makes the enemy move
if(Vector2.Distance(transform.position, player.position) > nearDistance){
transform.position = Vector2.MoveTowards(transform.position, player.position, walkSpeed * Time.deltaTime);
} else if(Vector2.Distance(transform.position, player.position) > stoppingDistance){
transform.position = Vector2.MoveTowards(transform.position, player.position, walkSpeed * Time.deltaTime);
} else if(Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > nearDistance){
transform.position = this.transform.position;
}
if(player.position.x > transform.position.x && transform.localScale.x > 0
|| player.position.x < transform.position.x && transform.localScale.x < 0)
{
Flip();
}
//Makes the enemy shoot
if(timeBtwShots <= 0){
GameObject newBullet = Instantiate(bullet, firePoint2.position, Quaternion.identity);
newBullet.GetComponent<Rigidbody2D>().velocity = new Vector2(shootSpeed * -walkSpeed * Time.fixedDeltaTime, 0f);
timeBtwShots = startTimeBtwShots;
} else {
timeBtwShots -= Time.deltaTime;
}
}
void Flip()
{
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
walkSpeed *= 1;
}
void Die ()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float dieTime, damage, speed;
public GameObject deathEffect;
// Start is called before the first frame update
void Start()
{
StartCoroutine(CountDownTimer());
}
IEnumerator CountDownTimer()
{
yield return new WaitForSeconds(dieTime);
Die();
}
void OnCollisionEnter2D(Collision2D col)
{
Die();
}
void Die()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
For the bullets not showing I bet is the ordering layer, that’s not the layer mask at the top right of the game object, it’s inside the sprite options.
If you want to make the bullets go in the same direction as the enemy you must use the enemy transform for that, I don’t have unity at hand now but I think it’s transform.localScale, I will update later.
Edit: that’s is, to fire in the same direction the shooter is facing,
bullet.transform.localScale = shooter.transform.localScale;
That’s awesome thank you very much, would I enter that in the enemy script or on the actual Bullet script?
I don’t think it’s an order layer problem as the order layer on the bullet higher than the rest of the scene. Also they do show up in game view but won’t appear until I walk my Player past the enemy position, then they show up normal. It’s a weird glitch
Go to the Sorting Layers sections, you can access there simply by trying to add a new layer and there you have different options: Tags, Sorting Layers, and Layers. Sorting Layers is what is used to render the objects in order, if you have:
0 Player
1 Background
2 Enemy
If your player’s sprite renderer has Sorting Layer Player and the background the Background Sorting Layer, the player will be renderer behind the background and you won’t see it in the game. Also, if 2 objects have the same Sorting Layer and same Order in Layer in the Sprite Renderer, I think they have that weird blinking effect. Anyway, just make sure you are doing right there.
In my case, the shooter has a reference of the bullet (actually is a holder with several bullets) so when the enemy shoots I update it with:
[SerializeField] protected Transform bulletHolder;
…
this.bulletHolder.localScale = this.transform.localScale;
I have seen different approaches to that, in the Update method, when you change the direction of the enemy… I prefer to do it when the enemy shoots.