Search Unity

sprialing ball

Discussion in 'Getting Started' started by sixdux8, Nov 13, 2019.

  1. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    Hi, superNoob here;

    I am following a book (Learning C# Programming With Unity 3D By Alex Okita[AKD]) trying to learn C# w/ Unity. Bellow is what I am instructed to put in the script( I am using Microsoft Visual Studio).

    public class NewBehaviourScript : MonoBehaviour
    {
    public float Control;
    public float OtherControl;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float x = Mathf.Sin(Control) * OtherControl;
    float y = Mathf.Cos(Control) * OtherControl;
    float z = Control * OtherControl;
    transform.position = new Vector3(x, y, z);
    }
    }

    I do so and change Control and OtherControl values in the inspector. The sphere jumps when I change the values, just jumps from one spot to another. The book says "Play with this for a little bit; just make sure OtherControl is a value other than 0. You should see a spiraling ball moving around, thanks to Mathf.Sin() and Mathf.Cos(). Basically, these two Mathf functions return the sine and cosine of control." yet the ball does not spiral or move acros the screen. Am I understanding this wrong? should the ball just jump or should it actually move and spiral.
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Please use code formatting when posting code. Most of us will ignore posts that simply paste unformatted code.

    What values are you entering for Control and OtherControl in the editor?
     
  3. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    OK, I will use code formatting in the future.
    I did not use the editor. In the Inspector(New Behavior Script tab that was created when I dragged the script over the ball/sphere) I used any number of values for Control and OtherControl. 0 to 9 for Control and 1 to 9 for OtherControl. The ball just jumps around when i change values.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The ball would only spiral if the value of Control were changing every frame. I see no code to make it do that. Either you or the author (or possibly the printer) have made a mistake. Go back and double-check your code against the book carefully; and if they're still the same, then see if you can fix it yourself, by putting in one more line of code in Update() that adds a little bit to Control.
     
  5. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    I have copied the code from the PDF book and checked it a few times over to confirm it is the same. I can see what you mean by putting in one more line of code in Update(), unfortunately i don't know how :)
    Can you suggest a C# book for total beginners?
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    While I respect that you are one of the few people who like to read books (I am too!), I don't know a good C# one, though I bet a few minutes of browsing and reading reviews at Amazon would turn one up.

    But you could certainly do worse than working your way through the Learn CS tutorial online.

    And incidentally, you're smart to realize that this is the next step. Unity is not a drag-and-drop development environment; you have to write code to get anything done. So learning at least the basics of C# is a necessity.

    Good luck, and post again if you get stuck!
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You mentioned "I used any number of values for Control and OtherControl. 0 to 9 for Control and 1 to 9 for OtherControl." but your code doesn't show that. Where are you setting the values?

    What if you tried

    float x = Mathf.Sin(Control) * OtherControl;
    float y = Mathf.Cos(Control) * OtherControl;
    float z = Control * OtherControl;
    transform.position = new Vector3(x, y, z);
    Control += 1.0f;
     
  8. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    I have put those numbers in the public float Control and OtherControl that opens up in the Inspector, not in the script. I will try as you suggest.
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I just checked out the book on Amazon, do you have the Github link for the files mentioned in the book?
     
  10. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    Hey! it worked the ball started spiraling in one direction, at +=1.0f the ball moves very fast but allways in one direction on the Z axis. Chaning it to +=0.2f makes the ball spiral much slower but on the same axis. Cool, thank you! Can you explain in a few short words what the 'f' represents?

    No, i dont have any links from the book. I'm only in chapter 3 but the book gives me no links/files to download, appart from instructing me to dl unity3d in chapter 2.
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  12. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    got it thx :)