Need some help trying to Instantiate correctly

Hi all, I’m still relatively new to c# so bare with me.

I’m trying to make an enemy character shoot a projectile. I had it all set up and working perfectly fine with damage + animations and everything.

However I wanted to make a prefab of this enemy and flip it (scale x) so that it shoots to the opposite direction. I’ve spent the last 3 hours trying to get this to work and with every solution, a new problem arose. (The projectile was facing the wrong way, or facing correct way but moving opposite way etc)

I have one combat script for my enemy which is responsible for instantiating the projectile. And I have a script for the projectile itself. Here’s what it looks like at the moment.

The code to instantiate the projectile:

private void Shoot()
{
    if (timeToFire <= 0f)
    {
        Instantiate(antSpitPrefab, shootPoint.position, shootPoint.rotation, this.transform);
        timeToFire = fireRate;
    }
    else
    {
        timeToFire -= Time.deltaTime;
    }
} 

The projectile code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class antSpit : MonoBehaviour
{
    [Range(1, 10)]
    [SerializeField] private float speed = 10f;

    [SerializeField] private float lifeTime = 5f;

    [SerializeField] protected float damage = 1f;


    
    // private float direction;


    private Rigidbody2D rb;
    public Animator animator;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();

        Destroy(gameObject, lifeTime);

        
    }



    private void FixedUpdate()
    {
        
        rb.velocity = transform.right * speed;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Player"))
        {
            animator.SetTrigger("Explode");
            speed = 0;
            collision.GetComponent<PlayerCombat>().TakeDamage(damage);
        }
    }

    private void Destroy()
    {
        Destroy(gameObject);
    }


} 

Any advice to make it instantiate relative to the enemy’s scaleX so that the projectile is always moving + facing the correct way? Thanks in advance

Instead of flipping your enemy using scale you can flip it by rotating it 180 degrees around the Y axis. This will then also rotate the shoot position and shoot rotation so then the bullet will instantiate with the correct orientation.

But instead of rotating the enemy with transform.rotation or transform.eulerAngles we can set its transform.right like this:

if (rb.velocity.x>0.01f)
    transform.right=Vector3.right;
else if (rb.velocity.x<-0.01f)
    transform.right=Vector3.left;

BTW - you don’t need to set the bullet’s velocity every frame. Just set it once and the bullet will continue to move until it hits something.

1 Like

…and change line 5 to:

Instantiate(antSpitPrefab, shootPoint.position, shootPoint.rotation);

You don’t want the bullets to be a child of the enemy.

1 Like

Hey! Thanks for the reply.

I moved the velocity from fixedupdate() to start(). Reckon that’s better?

And the rotation code you provided, should that go in the Enemy script or the projectile? I tried to put it in the projectile script, in Start(), right underneath the code that sets the velocity. And that didn’t really seem to change anything. Here’s how the projectiles appear with this code:

As for the projectiles, I figured making them a child of the enemy that casts it is a good start to try and orient them according to the enemy that cast it. If I remove it, won’t that make each projectile separate from the enemies?

Sorry if I was unclear on my initial post, I’m trying to make more than one enemy that shoots in different directions (as you can see from the image) the top left enemy is shooting a projectile which functions correctly, but the bottom right enemy, even though the projectile is facing the correct way, its moving backwards.

The code should go in the enemy script. But it’s only to give you an idea on how to flip your enemy. For example, your current script may not flip your enemy in relation to its current velocity.

1 Like

Hmmm. My enemies don’t currently have a rigidbody nor do they move at all. They just sit there and shoot the projectiles. And yeah this seems like an easy way to flip the enemies but my problem is more the projectiles than the enemies themselves.

So then you don’t need my code at all. Instead you can just edit the enemies in the editor and instead of flipping them with scale you set their Y rotation to 0 or 180 depending on which way you want them to face.

1 Like

I really appreciate you trying to help but yeah your code would be indeed helpful if I needed a way to flip the enemies… But as I keep saying… The projectiles are my issue and the projectiles are the ones not being oriented correctly, not the enemies.

Your projectile script is fine. You just need to orientate the enemy correctly by setting their Y rotation.

1 Like

So after experimenting a little more with your advice I realised that the bullet doesn’t have an independent trajectory (wow). All this time I thought the enemy just holds the location where it spawns and the velocity of the bullet is getting handled solely by the bullet script. That really blew my mind I didn’t even consider this possibility. Anyway the solution didn’t need any extra code at all, I just rotated the top left enemy 180 around Y axis and in the velocity script of the bullet, just made the velocity value negative:

rb.velocity = -transform.right * speed;

Now both enemies are shooting in the correct direction.
Thanks a lot for the advice!
My ignorance failed to let me consider the enemy’s rotation as a possible solution.