Help with "laser bullets"

So I’m making a “top down” game, not fully top but more from the side.

I’m having 2 issues with my laser gun.

first issue

(my laser is a prefab) when it Instantiates in, it does not hold the “rotation” of the prefab. so its shooting sideways lol.

also, I can only shoot in 1 direction with “forward” only along the x axis. How can I shoot “outwards” from the spawn point? I’m looking for a different command but can find one in the Vector3 api.

Also I’m a newbie, but I have taken a few courses so I’m not that Fresh haha.

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

public class Laser : MonoBehaviour
{
    [SerializeField] int speed;
    [SerializeField] float distance;  
   
   
    // Update is called once per frame
    void Update ()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
        distance += 1 * Time.deltaTime;

        if (distance >= 2.5f)
            Destroy(this.gameObject);

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


public class LaserRound : MonoBehaviour
{

    public GameObject laserSpawnPoint;
    public GameObject laserPrefab;
    public float waitTime;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        // if q is pressed
        //spawn laserprefab at player position

        if (Input.GetKeyDown(KeyCode.Q))
        {
            Instantiate(laserPrefab.transform, laserSpawnPoint.transform.position, Quaternion.identity);


        }
    }


}

Your code does not perform any rotations. Check out the section here where it talks about instantiating projectiles.

Ok so this is the new code I have, but now it seems even more messed up lol.

so the projectile is now spawning in the correct direction, just not spawning in the correct x,y. and still not doing the correct rotation?

here is the code for the player

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


public class LaserRound : MonoBehaviour
{
    public Rigidbody projectile;
    public GameObject laserSpawnPoint;
    public float waitTime;

    // Use this for initialization
    void Start () {
      
    }
  
    // Update is called once per frame
    void Update ()
    {
        // if q is pressed
        //spawn laserprefab at player position

        if (Input.GetKeyDown(KeyCode.Q))
        {
            Rigidbody clone;
            Instantiate(projectile.transform, laserSpawnPoint.transform.position, Quaternion.identity);
            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }
    }

here is the code that is on the projectile (laser)

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

public class Laser : MonoBehaviour
{
    [SerializeField] int speed;
    [SerializeField] float distance; 
  
  
    // This is the scrip that is on the projectile prefab
    void Update ()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
        distance += 1 * Time.deltaTime;

        if (distance >= 2.5f)
            Destroy(this.gameObject);

    }
}

and here are pictures so because I know typing out what I mean, doesn’t usually happen for me lol.
so there are the pictures of my lasers ( see how they are sideways but the prefab laser is at a 90* angle)


Setting the rotation at instantiation overrides any prefab value, and Quaternion.identity is 0,0,0 not “no value” or something like that. If you want to inherit the prefab’s rotation, you need to not explicitly set it in the clone.

You seem to also be mixing rigidbody physics and transform movement – you set the velocity when you create the projectile but then in the Laser script, you move the transform manually? Pick one or the other (usually the rigidbody velocity so you get accurate physics).

Finally, you have two Instantiate calls. Why’s that?

1 Like

Adding more: If your laser is rotated the wrong way because your mesh points straight up but you want it to face forward, add the actual laser mesh as a child to an empty GameObject and rotate it there. Then you can orient the parent down the muzzle of the gun and the child (the laser mesh) will keep its rotation.

1 Like

and I’m completely wrong, thank you for pointing that out

All fixed then? Let us know if you have any other issues.

can you explain what you mean here
“You seem to also be mixing rigidbody physics and transform movement”

You have a rigidbody attached to your object, and you set its velocity:

 clone.velocity = transform.TransformDirection(Vector3.forward * 10);

But then in your Laser script, you have:

transform.Translate(Vector3.forward * Time.deltaTime * speed);

So you’re using two different systems of movement: the physics system and manual transform manipulation. You should use one or the other but not both.

2 Likes

ohhhh ok! thank you for helping out GroZZler

I learned ALOT just from this little post, thank you.

2 Likes

I have to say Grozzlers very good at explaining quite technical stuff and doesn’t waste time doing it.

Edit: Sorry, that was a different person, there are lots of helpful people on here and I get them confused :smile: that one was leftyrighty though I swear I’ve seen Grozzler do some similar posts before too.

Thanks mate. Appreciate the kind words.