Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved [RESOLVED] Bugs, I NEED HELP

Discussion in '2D' started by SpyderManToo, Feb 16, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    I added rigidbody movement to my game and now shooting is weird.
    When I shoot while moving, the bullet curves weirdly and when i stop moving, they go back to regular
    heres my shooting code, bullet code, and movement code

    shooting code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerShoot : MonoBehaviour
    6. {
    7.     public Transform firePoint;
    8.     public Rigidbody2D bullet;
    9.     public float fireSpeed = 500f;
    10.  
    11.     private const float gunCooldown = 1f;
    12.     float cooldown;
    13.  
    14.  
    15.     private void Start()
    16.     {
    17.         cooldown = gunCooldown;
    18.     }
    19.     private void Update()
    20.     {
    21.         cooldown -= Time.deltaTime;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void LateUpdate()
    26.     {
    27.         if (cooldown <= 0)
    28.         {
    29.             Fire();
    30.         }
    31.         else
    32.         {
    33.             return;
    34.         }
    35.     }
    36.  
    37.     void Fire()
    38.     {
    39.         if(Input.GetButtonDown("Fire1"))
    40.         {
    41.             var firedBullet = Instantiate(bullet, firePoint.position, firePoint.rotation);
    42.             firedBullet.AddForce(firePoint.up * fireSpeed);
    43.             cooldown = gunCooldown;
    44.         }
    45.     }
    46. }
    47.  
    bullet:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     float bulletDestroyTime = 2f;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         Invoke(nameof(DestroySelf), bulletDestroyTime);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.     }
    21.  
    22.     void OnCollisionEnter2D(Collision2D collision)
    23.     {
    24.         DestroySelf();
    25.     }
    26.  
    27.     void DestroySelf()
    28.     {
    29.         Destroy(gameObject);
    30.     }
    31.  
    32. }
    33.  
    movement:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.  
    9.     Rigidbody2D playerRb;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         playerRb = GetComponent<Rigidbody2D>();
    15.         transform.position = new Vector3(0, 0, 0);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         RigidbodyMovement();
    22.  
    23.     }
    24.  
    25.     void RigidbodyMovement()
    26.     {
    27.         float xMove = Input.GetAxisRaw("Horizontal");
    28.         float yMove = Input.GetAxisRaw("Vertical");
    29.  
    30.         playerRb.velocity = Vector2.right * xMove * moveSpeed;
    31.         playerRb.velocity = Vector2.up * yMove * moveSpeed;
    32.     }
    33. }
    34.  
    also, I cant move on the x axis, only on the y axis

    heres my rigidbody

    Screen Shot 2021-02-16 at 12.18.52 PM.png
     
  2. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    ok so the reason why the bullet curves weirdly is because it moves proportionally to the direction of the way im facing but it isn't the direction of when i shoot it, its the current direction, meaning that i can steer the bullet. how do i get rid of this stupid bug

    also why cant i move on x axis
     
  3. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    huh but thats also weird cuz its when i move and not when i look.
     
  4. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
    Is the bullet parented to the player or firepoint after instantiation?
     
  6. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    No, could u explain why i need to do that?
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
    You shouldnt need to. I was just wondering if it was doing that. The only thing i can think of with regards to your scripts is the 2nd line in your Fire() function. The part where it does AddForce(firePoint.up * speed) is making me think that the firePoint is somehow changing when moving. Can you add a Debug.Log after that line with something like:

    Debug.Log("The firePoint value is " + firePoint);

    Im hoping it only calls once per click but maybe it is doing it multiple times. Let me know if that Debug.Log is calling more than once per click.
     
  8. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    well, i added it, im not sure what it does, ive decided just to switch to raycasts
     
  9. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    ok that didn't work, im back on bullet instantiation
    heres my shoot script




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

    public class Shooting : MonoBehaviour
    {
    public Transform firePoint;
    public GameObject bulletPrefab;

    public float bulletForce = 20f;

    // Update is called once per frame
    void Update()
    {
    if (Input.GetButtonDown("Fire1")) //If left click, shoot
    {
    Shoot();
    }
    }

    void Shoot()
    {
    GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //Name the bullet and instantiate it
    Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>(); //Get the rigidbody of the bullet
    rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse); //Apply an instant force to the bullet to launch it in the direction we are facing
    }
    }



    heres my bullet script

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

    public class Bullet : MonoBehaviour
    {
    private void OnCollisionEnter2D(Collision2D collision)
    {
    Destroy(gameObject);
    }
    }
     
  10. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    ok i figured it out, turns out it was just how it looked because of my camera follow script, the bullets dont actually curve