Home>

I want to fly a bullet straight, but when I hit the bullet in a normal state, it will fly straight, but when jumping, it will fly diagonally.
What amendments will make you fly straight at any time?

** Jump processing **
if (isJump == false&&gameClear == false&&WaitStop == false)
        {
            // Jump in space.
            if (jumpCount<MAX_JUMP_COUNT&&(Input.GetKeyDown ("space") || Input.GetKeyDown ("joystick button 0")))
            {
                isJump = true;
                anim.SetBool ("Charge", false);
                anim.SetTrigger ("Jump");
            }
        }
        float velY = rigidbody2D.velocity.y;
        bool isJumping = velY>0.1f? true: false;
        bool isFalling = velY<-0.1f? true: false;
        anim.SetBool ("isJumping", isJumping);
        anim.SetBool ("isFalling", isFalling);

        if (isJump)
        {
            // The second jump after clearing the speed will behave the same as the first jump.
            rigidbody2D.velocity = Vector2.zero;
            // Jump.
            rigidbody2D.AddForce (Vector2.up * force);
            // Count the number of jumps.
            jumpCount ++;
            // Allow jumps.
            isJump = false;
            // anim.SetBool ("Jump", true);
        }
Code
** Add this action to the shooting animation **
  void Bullet () // generate a bullet
    {
        Instantiate (bullet, transform.position + new Vector3 (0f, 1.2f, 0f), transform.rotation);
    }
Code
** Scripts added to the generated bullet prefab **
using UnityEngine;
using System.Collections;
public class BulletScript: MonoBehaviour
{
    private GameObject player;
    private int speed = 10;
    void Start ()
    {
        player = GameObject.FindWithTag ("UnityChan");// Get Unity-chan object
        Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();// Get rigidbody2D component
        // Shoot the bullet in the direction of Unity
        rigidbody2D.velocity = new Vector2 (speed * player.transform.localScale.x, 0);
        // Align image orientation with Unity
        Vector2 temp = transform.localScale;
        temp.x = player.transform.localScale.x;
        transform.localScale = temp;
        // 5 seconds later
        Destroy (gameObject, 5);
    }
    // ********** Start ********** //
    void OnTriggerEnter2D (Collider2D col)
    {
        if (col.gameObject.tag == "Enemy") // Delete bullets when colliding with Enemy
        {
            Destroy (gameObject);
        }
    }
    private void OnCollisionEnter2D (Collision2D col)
    {
        if (col.gameObject.tag == "Enemy") // Deletes bullet if it collides with Enemy
        {
            Destroy (gameObject);
        }
    }
    // ********** End ********** //
}
Code