Search Unity

How to make bullet speed be character speed + bullet speed?

Discussion in '2D' started by the_cubic_cat, Jun 14, 2020.

  1. the_cubic_cat

    the_cubic_cat

    Joined:
    Jun 14, 2020
    Posts:
    1
    I know this is probably a very basic thing, but how do I make it so when i press the shoot button the speed of the bullet is character speed + bullet speed, like in real life? I know that i can just write that through code, but i want to know if there is a built-in solution. Anyway, here's my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletController : MonoBehaviour
    6. {
    7.     public float initialSpeed = 10.0f;
    8.     public Transform particles1;
    9.     public Transform particles2;
    10.     public int direction = 1;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         gameObject.GetComponent<Rigidbody2D>().AddForce(transform.right * direction * initialSpeed, ForceMode2D.Impulse);
    16.     }
    17.  
    18.     private void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.gameObject.CompareTag("Ground"))
    21.         {
    22.             Destroy(gameObject);
    23.         }
    24.     }
    25. }
    26.