C# : How to add velocity to a bullet and let it rotate with the player

Okay so im having a slight issue figuring out how to make a projectile move without effecting its rotation.

My script:

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

public class PlayerController : MonoBehaviour {

    public float speed = 5.0f;
    public GameObject bullet;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        if(Input.GetKey(KeyCode.Mouse0)){
            Instantiate(bullet, transform.position, transform.rotation);
        }

        if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.Q)){
            transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
        } else if (Input.GetKey(KeyCode.D)){
            transform.position += new Vector3(+speed * Time.deltaTime, 0, 0);
        } else if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.Z)){
            transform.position += new Vector3(0, +speed * Time.deltaTime, 0);
        } else if (Input.GetKey(KeyCode.S)){
            transform.position += new Vector3(0, -speed * Time.deltaTime, 0);
        }

        {
        Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }
    }
}

This is the part where I am instantiating my bullet:

void Update () {
        if(Input.GetKey(KeyCode.Mouse0)){
            Instantiate(bullet, transform.position, transform.rotation);
        }

As you can see it rotates towards the players rotation and now im just stuck on how to get the pixel of doom to fly. Can anyone help me out?

Firstly you should fix your Instantiation. That will spawn a new bullet every single frame you’re holding down left click. Use GetKeyDown or GetMouseButtonDown just for the first click, or add a delay before spawning a new one.

To make the bullet move, maybe add a Bullet script to it that moves it in its forward vector on Start.

Yeah just noticed the bullet spawn madness. How would I go about adding a delay and movement for the bullet? Im kinda just slapping these things together as I go.

EDIT: Nvm on the delay the Getkeydown is working fine for what I want.

Can anyone help me with getting my bullet to move when spawned? I have been struggling for days trying different things but I just cant seem to get it to work.

Bullet has a rigidbody?

GameObject b = Instantiate(bullet, transform.position, transform.rotation);
b.GetComponent<RigidBody>().AddForce(transform.forward * 1.234f);

Will give it a shot but wont the rigidbody collide with the player as it gets spawned on the player? Im at work at the moment will check when I get home. I just realized I forgot to mention its a 2D top down shooter

Separate the objects into layers.

Create a:
Player layer ( The player will be in this layer)
Projectiles layer ( All player shoot projectiles go here)
and a enemys layer ( I guess you have enemys if you shoot so)

Now program it so that

player collides with projectile = nothing happens.
enemy collides with projectile = enemy dies ( or takes dmg or whatever you want).
enemy collides with player = player gets dmg.

projectile collides projectile = nothing
etc

I hope this helps

https://docs.unity3d.com/Manual/LayerBasedCollision.html
:slight_smile:

okay so this is beginning to frustrate me so much :confused: Not quiting though. So I modified the script to Zaflis suggestion and its still not working :frowning: Posting screenies and shizz.

So the script atm:

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

public class PlayerController : MonoBehaviour {
    
    public float speed = 5.0f;
    public GameObject bullet;
    
    // Use this for initialization
    void Start () {
                  
    }

    private void Awake(){
       
    }
      
    // Update is called once per frame
    void Update () {
        if(Input.GetKey(KeyCode.Mouse0)){
            GameObject b = Instantiate(bullet, transform.position, transform.rotation);
            b.GetComponent<Rigidbody>().AddForce(transform.forward * 1.234f);
        }
    
        if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.Q)){
            transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
        } else if (Input.GetKey(KeyCode.D)){
            transform.position += new Vector3(+speed * Time.deltaTime, 0, 0);
        } else if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.Z)){
            transform.position += new Vector3(0, +speed * Time.deltaTime, 0);
        } else if (Input.GetKey(KeyCode.S)){
            transform.position += new Vector3(0, -speed * Time.deltaTime, 0);
        }
    
        {
        Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        }
    }
}

Le bullet:

Le player:

So I have no idea what im doing wrong. LeftyRighty help a brother out :confused:

“Not working” isn’t very helpful. What isn’t working about it?

The only potential issue I see is that you probably want transform.right, not transform.forward, if you’re working in 2D.

ah in my frustration I must have skipped to say what isnt working. Okay so the bullet isnt moving at all, it spawns and then it just stays there. Its a 2D Top Down Shooter the Player rotates by following the mouse and the bullet also rotates based on the players rotation. At the moment the only thing not working is the bullet not moving at all.

bump

If it’s 2D you need to be using RigidBody2D’s and all Collider…2D’s in code and inspector. Try bigger force, i tried to make the number 1.2345… look like it’s just a rough example. Test different numbers like 100 or 1000. Then debug all collisions it makes with anything.

(And as he said, forward vector may not be good in 2D if it’s meaning depth axis.)

Example on how to add collision event Unity - Scripting API: Collider2D.OnCollisionEnter2D(Collision2D)