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

How do I rewrite this older code with GetComponent instead?

Discussion in 'Scripting' started by fgbg, Nov 3, 2017.

  1. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Noob here. Trying to understand how to write the below code in a newer version of Unity. I need to access the rigidbody of the projectile, not of the object that the script is on:

    GameObject projectile = (GameObject) Instantiate( bullet, myPos, rotation);
    projectile.rigidbody2D.velocity = direction * speed;


    I get the error:

    Severity Code Description Project File Line Suppression State
    Error CS0619 'GameObject.rigidbody2D' is obsolete: 'Property rigidbody2D has been deprecated. Use GetComponent<Rigidbody2D>() instead. (UnityUpgradable)' Game #1.CSharp C:\...\Gun.cs 42 Active
     
    Last edited: Nov 3, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    projectile.GetComponent<Rigidbody2D>().velocity

    It does show you in the error message as well :)
     
    fgbg likes this.
  3. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Ah ok, I actually tried that but did not add the "()" at the end. I ended up putting it in two lines like so:

    // Get the bullet's rigidbody
    rb = projectile.GetComponent<Rigidbody2D>();

    // Set the rigidbody's velocity
    rb.velocity = direction * 1;

    Any idea how to set the bullet's velocity so that it is not affected by the distance between my mouse and my player? The closer they are, the slower it shoots... which makes sense the way it's coded now. And also, how would I find the answer to this on my own?
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Normalize your vector.

    Code (csharp):
    1.  
    2. direction.normalized * 1f; // may want to increase this
    3.  
     
    fgbg likes this.
  5. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Thanks! So now I should be able to access my ship's velocity and add that into the velocity somehow too, right? bullets start going slower than my ship if I'm moving in the same direction I'm firing.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Sure - just add your ship's velocity to the projectile's.

    Code (csharp):
    1.  
    2. projectileRigidbody.velocity =  shipRigidbody.velocity + (direction.normalized * 1f);
    3.  
     
  7. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Ah simpler than I thought thank You!

    Btw, are there tutorials or classes that go over Unity's functions, particularly math based ones? I jist don't understand the math enough to know what I'm even looking for sometimes.

    For instance, how the hell are velocities simply added together? Aren't they multiple values?
     
  8. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
  9. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  10. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    Yeah so I know to reference Vector3 on the unity site, but I don't think I'd ever even know about Vector3 unless a tutorial used it.

    Secondly... Ok vectors. Now what? What else am I missing. I need something like a "math for game development" course or something.

    Thirdly, now what about ALL functions, even non math ones? Am I expected to just dig through each one on the unity site to get a feel for them?
     
  11. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    You dont use them all...
    This stuff is usually on a need-to-know basis. You cant memorise every function...
     
  12. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Precisely. Keep watching tutorials and learning how different things work, and over time you'll get a grip on all the common knowledge you'll need. At least to google effectively.

    Math for Game Developers:
    https://www.youtube.com/playlist?list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My
     
    fgbg likes this.
  13. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    fgbg likes this.
  14. fgbg

    fgbg

    Joined:
    Dec 22, 2012
    Posts:
    53
    So I got this to work with something like this

    void Start () {
    gunRB = GetComponentInParent<Rigidbody2D>();
    playerRB = gunRB.GetComponentInParent<Rigidbody2D>();
    }
    ...
    bulletRB.velocity = playerRB.velocity + (direction.normalized * BulletSpeed);

    However this didn't solve the problem like I thought it would. If I drive my ship forward and shoot, the bullets go way too fast. If I drive backwards while shooting forward, the bullets almost stand still.

    Is there some kind of game design trick I'm missing here?
     
  15. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    No - that's expected. If you throw a baseball 30 mph backwards out of a car moving 30 mph forwards, it just drops straight to the ground.

    Maybe you want something more arcade-y and don't want to add the ship's velocity after all?