Hi everyone!
Well I made everything that the video says but my enemies keep disapearing and not shooting at all.
Here I paste the EnemyBehaviour script.
public class EnemyBehaviour : MonoBehaviour {
public GameObject projectile;
public float health = 150f;
public float projectileSpeed = 10f;
private void Update()
{
Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
GameObject missile = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
missile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
}
void OnTriggerEnter2D(Collider2D collider)
{
Projectile missile = collider.gameObject.GetComponent<Projectile>();
if (missile)
{
health -= missile.GetDamage();
missile.Hit();
if (health <= 0)
{
Destroy(gameObject);
}
}
}
I hope you can help me!
I notice an odd behaviour. When the game Starts, the missiles of the enemy appears in the Hierarchy. Later they get destroyed themselves. There is no shredder at the bottom.
I am using Unity 2017, thats why the GetCompomente of the Rigidbody is different.
Thanks!
Hello there, what may be happening is that the missiles are colliding with the enemy when it get instantiated, make sure to uncheck the physics collision between them or make sure to use an if statement that makes sure the projectile is the one coming from the player
Hi! Well the problem it’s not that, I debugged and I realize that the problem is that the projectiles exists, but they are not showing in the screen.
The curious thing is that yesterday the PlayerLaser works fine, but now have the same behaviour than the EnemyLaser. I am changing the sprites, but it’s not seems to work either.
Thanks for answering!
News! The problem is that the projectile is not being fired. They appear in the position of the player. But i still can’t see it.
I think that the problem is in this scripts, because now i figured out how to solve the problem of the player, but this keeps having the same behaviour. I think that is the offset in the Fire, but I don't know for sure.
public GameObject projectile;
public float health = 150f;
public float projectileSpeed = 10f;
public float shotsPerSeconds = 0.5f;
private void Update()
{
float probability = Time.deltaTime * shotsPerSeconds;
if (Random.value < probability)
{
Fire();
}
}
void Fire()
{
Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
GameObject missile = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
missile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
}
void OnTriggerEnter2D(Collider2D collider)
{
Projectile missile = collider.gameObject.GetComponent<Projectile>();
if (missile)
{
health -= missile.GetDamage();
missile.Hit();
if (health <= 0)
{
Destroy(gameObject);
}
}
}
I solved the player issue and the enemy issue of firing. But now they fire at the rate given but the missiles appears VERY offset. I mean, really far from the shooter. And sometimes they don-t appear at all (i can see it in the Hiearachy)
1 Like
nice solutions,
Does the projectile prefab have some sort of child with the sprite or something? You are instantiating the prefab in the transform.position by the way, seems that should be at the startPosition (but this wouldn’t cause the offset problem)
Solved! The problem was that Vector of startPosition was in (0, 1, 0) and it should be at (0, -1, 0 ).
I realize when I see that the instantiate was good (I made the change you say putting only startPosition) but the position wasn’t.
Thanks a lot for your help!
1 Like
Glad to know that you found the solution for this problem 