Search Unity

I can't give my instantiated object a velocity???

Discussion in 'Scripting' started by samdaman93, May 1, 2020.

  1. samdaman93

    samdaman93

    Joined:
    Oct 3, 2015
    Posts:
    37
    Hello,

    I've delved through a few code iterations and googled around the net, I'm yet to find a way to get this to work. I've posted my code below.

    I've got my game object attached to the script and the instantiation point transform (fireballPoint). If I give the fireball velocity on the fireball script it works fine, it just seems to be when I set it from the player.

    How would I go about doing that?

    Code (CSharp):
    1.  
    2. public GameObject fireball;
    3.     public Transform fireballPoint;
    4.     public float fireballSpeed = 10f;
    5.  
    6. void attack()
    7.     {
    8.         GameObject Fireball = Instantiate(fireball);
    9.         Fireball.transform.position = fireballPoint.transform.position;
    10.         Rigidbody2D Fireballrb = Fireball.GetComponent<Rigidbody2D>();
    11.         Fireballrb.velocity = new Vector2(inputHorizontal * fireballSpeed, Fireballrb.velocity.y);
    12. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    What debugging steps have you tried? If I were you I would try a logging statement after line 10:

    Code (CSharp):
    1. Debug.Log($"inputHorizontal is {inputHorizontal}, fireballSpeed is {fireballSpeed}");
    if either one of these is zero, your resulting x velocity component will be zero too.

    Also your variable names are scary to me! I'd be so worried about mixing up "fireball" and "Fireball". If I were you I'd name the prefab "FireballPrefab" to avoid any confusion!
     
    Joe-Censored likes this.
  3. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    Also, variable names should start from lower case.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add debugging as already mentioned for all variables where you set velocity, but also verify you don't have any errors in the console. If line 10 is returning null, then you won't see any velocity from line 11, and you will have a null reference error recorded in the console even if all those other variables have "good" values.

    That's just coding convention, is highly dependent on the coding standards implemented by your team, and not actually a software bug.
     
  5. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    Yes, not a software bug and not mandatory. But AFAIK this coding convention is quite standard in C#.
     
  6. samdaman93

    samdaman93

    Joined:
    Oct 3, 2015
    Posts:
    37
    Haha thanks for all the advice, I'm very new so trying to learn the proper conventions as I'm going.

    I've added the debug line. I see whats happening now. It takes the current value of inputHorizontal i.e if I'm not inputting a direction, the fireball doesn't move. I'm going to set a variable that is essentially direction which will be changed when the character changes direction.

    This debug option is awesome, I didn't know you could use it like that! thank you!
     
    PraetorBlue and Joe-Censored like this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah you should incorporate it as your go to method for investigating pretty much any issue. Debug.Log has a small performance hit, so you usually want to remove them or comment them out when you're done if they are getting called frequently.
     
    samdaman93 likes this.
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you're referring to putting variables within the debug message, that's called string interpolation, and it works anywhere you have to build a string. Just put a $ before the " and then anything you put in {brackets} will be treated as code, with the result injected into the string. You can even add string format parameters like this:
    Code (csharp):
    1. someTextLabel.text = $"Some text and then this, {someFloat:#.00} with two decimal places after the dot.";
     
    Joe-Censored and samdaman93 like this.