Search Unity

My bullet is shooting the wrong way when i face left im using the right analog to aim

Discussion in '2D' started by lukekillings, Mar 6, 2018.

  1. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    I think its got something to do with vector 2 getting flipped when my player turns left
    this is my bullet code so far


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class projectileforce : MonoBehaviour {
    public float shootspeed = 0;
    // Use this for initialization
    void Start () {
    transform.Translate(Vector2.right * 4F * shootspeed);
    }

    // Update is called once per frame
    void Update () {

    }
    }
     
    Last edited: Mar 6, 2018
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    You can make second bullet with: transform.Translate(Vector2.left * 4F * shootspeed); and spawn needed bullet depended on players face (I think, you have something "isFacingRight" in the Players script, then spawn bullet flying right, if it is true and spawn other bullet, if it is false).
     
  3. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    thank you I gave it a go with this
    void Update () {
    if (characterEngine.facingRight == true)
    transform.Translate(Vector2.right * 4F * shootspeed);
    if (characterEngine.facingRight == false)
    transform.Translate(Vector2.left * 4F * shootspeed);

    seems to work but I cant destroy the bullets because of ridgedbody
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Can't destroy them? Do you destroy them on impact and/or after a certain amount of time/distance?
     
  5. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    I tried by time and I'm getting an error about ridbody not getting called then its not letting me shoot at all thanks for the reply
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, what is the exact error, because I don't understand from the way you described it.

    Also, can you post the code in question, with the 'Destroy' call?

    Should be something like (on the script on the bullet):
    Code (csharp):
    1. void Start() {
    2.     Destroy(gameObject, 4); // or whatever time.
    3.   }
     
  7. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    ill get right on that thanks again ill post the error code ;)
     
  8. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    MissingReferenceException: The object of type 'Rigidbody2D' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:52)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:156)
    UnityEngine.Object.Instantiate[Rigidbody2D] (UnityEngine.Rigidbody2D original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206)
    gunshooting.FixedUpdate () (at Assets/gunshooting.cs:25)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well some code is trying to access a destroy rigidbody. Perhaps with your code posted, it'll be possible to spot where. Include the line number, as it appears in the posted code, where the error occurs, if one was given*. :)
     
  10. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    I think its from this
    using UnityEngine;
    using System.Collections;
    public class gunshooting : MonoBehaviour
    {
    public Rigidbody2D projectile;
    public float speed = 20;

    public float fireRate = 0.9f;
    public float nextFire = 0.9f;
    private void Start()
    {
    float rtrig = Input.GetAxis("rtrig");
    }
    // Update is called once per frame
    void FixedUpdate()
    {

    if (Mathf.Round(Input.GetAxisRaw("rtrig")) < 0)
    {
    if (Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    Rigidbody2D instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody2D;
    instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
    }
    }
    }
    }

    thanks again for all this help
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't see any code there talking about destroying the projectile. There aren't errors in this code, are there?
    If so, which line?

    If not, post the code (line/s) that do have the error.

    When posting code on the forums, please use code tags - which you can read about here: https://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  12. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    I just tried this seems to work

    if (gameObject.name == "projectile_0(Clone)")
    {
    Destroy(gameObject, 5);
    }
     
  13. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    I think its cus I was not using a prefab
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm, okay.. Sorry for the little bit of confusion.

    I maybe see what you meant there, because you cannot call destroy on a rigidbody exactly, but you could use this:
    Code (csharp):
    1. Destroy(instantiatedProjectile.gameObject, 5);
    If you add that line of code, after it's instantiated.
     
  15. lukekillings

    lukekillings

    Joined:
    Mar 1, 2018
    Posts:
    13
    thanks again dude with all the help ive only got 5 or so bullets in game at a time seems like that's fixing it now
    upload_2018-3-8_23-28-12.png
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool :) Enjoy your game.