Search Unity

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

Discussion in 'Scripting' started by DarkBladeNemo, Nov 22, 2016.

  1. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Okay so im having a slight issue figuring out how to make a projectile move without effecting its rotation.

    My script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed = 5.0f;
    8.     public GameObject bullet;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         if(Input.GetKey(KeyCode.Mouse0)){
    18.             Instantiate(bullet, transform.position, transform.rotation);
    19.         }
    20.  
    21.         if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.Q)){
    22.             transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
    23.         } else if (Input.GetKey(KeyCode.D)){
    24.             transform.position += new Vector3(+speed * Time.deltaTime, 0, 0);
    25.         } else if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.Z)){
    26.             transform.position += new Vector3(0, +speed * Time.deltaTime, 0);
    27.         } else if (Input.GetKey(KeyCode.S)){
    28.             transform.position += new Vector3(0, -speed * Time.deltaTime, 0);
    29.         }
    30.  
    31.         {
    32.         Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
    33.          float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    34.         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    35.         }
    36.     }
    37. }
    38.  
    This is the part where I am instantiating my bullet:

    Code (CSharp):
    1. void Update () {
    2.         if(Input.GetKey(KeyCode.Mouse0)){
    3.             Instantiate(bullet, transform.position, transform.rotation);
    4.         }
    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?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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.
     
  3. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    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.
     
  4. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    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.
     
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Bullet has a rigidbody?

    Code (CSharp):
    1. GameObject b = Instantiate(bullet, transform.position, transform.rotation);
    2. b.GetComponent<RigidBody>().AddForce(transform.forward * 1.234f);
     
  6. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    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
     
  7. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    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
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    https://docs.unity3d.com/Manual/LayerBasedCollision.html
    :)
     
  9. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    okay so this is beginning to frustrate me so much :/ Not quiting though. So I modified the script to Zaflis suggestion and its still not working :( Posting screenies and shizz.

    So the script atm:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.    
    7.     public float speed = 5.0f;
    8.     public GameObject bullet;
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.                  
    13.     }
    14.  
    15.     private void Awake(){
    16.        
    17.     }
    18.      
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if(Input.GetKey(KeyCode.Mouse0)){
    22.             GameObject b = Instantiate(bullet, transform.position, transform.rotation);
    23.             b.GetComponent<Rigidbody>().AddForce(transform.forward * 1.234f);
    24.         }
    25.    
    26.         if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.Q)){
    27.             transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
    28.         } else if (Input.GetKey(KeyCode.D)){
    29.             transform.position += new Vector3(+speed * Time.deltaTime, 0, 0);
    30.         } else if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.Z)){
    31.             transform.position += new Vector3(0, +speed * Time.deltaTime, 0);
    32.         } else if (Input.GetKey(KeyCode.S)){
    33.             transform.position += new Vector3(0, -speed * Time.deltaTime, 0);
    34.         }
    35.    
    36.         {
    37.         Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
    38.         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    39.         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    40.         }
    41.     }
    42. }
    43.    
    44.  
    Le bullet:



    Le player:



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

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    "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.
     
  11. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    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.
     
  12. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
  13. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    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 https://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html