Search Unity

Official Roll-a-ball Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Apr 17, 2015.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There seems to be an issue with the ball (and other physics objects) that slowly start to move even if there is no force applied. I'm looking into this.

    Is this the issue you are talking about?
     
  2. ua_ACCACIH

    ua_ACCACIH

    Joined:
    Apr 25, 2015
    Posts:
    5
    You quickly responded. Thank you.
    I found a possible answer:
    1) requires the editor to write the variables ("Speed") with an uppercase letter
    2) Line 17 changed "moveHorizontal + 0.1f"
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour {
    4.    
    5.     public float Speed = 10.0f;
    6.    
    7.     private Rigidbody rb;
    8.    
    9.     void Start(){
    10.         rb = GetComponent<Rigidbody>();
    11.     }
    12.    
    13.     void FixedUpdate(){
    14.         float moveHorizontal = Input.GetAxis("Horizontal");
    15.         float moveVertical = Input.GetAxis("Vertical");
    16.        
    17.         Vector3 movement = new Vector3 (moveHorizontal + 0.1f, 0.0f, moveVertical);
    18.  
    19.         rb.AddForce (movement * Speed);
    20.     }
    21. }
     
    jacobpayne89 likes this.
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    0.0!

    Great that you are trying to find answers to your problems.

    Some of them are not ... quite... the best choice.

    Variables should start with a lower case letter, so speed is more correct than Speed. The inspector will look at the case in the variable and correct it to be easier to read in the inspector. So, thisVariableName will read in the inspector as This Variable Name. Note that in the inspector, there will be spaces before each capital letter.

    If you have moveHorizontal + 0.1f, you will be adding additional force on the x-axis every frame, even if no keys are being pressed. The minimum value you can pass as movement will be (0.1, 0.0, 0.0) which is probably not what you want.
     
  4. ua_ACCACIH

    ua_ACCACIH

    Joined:
    Apr 25, 2015
    Posts:
    5
    at
    Code (CSharp):
    1. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    The ball moves in the real world. Slowly, then faster
    ********
    at
    Code (CSharp):
    1.  Vector3 movement = new Vector3 (moveHorizontal + 0.1f, 0.0f, moveVertical);
    The ball starts moving faster.
    =>>The video ("Roll a Ball Tutorial - 2 of 8: Moving the Player - Unity Official Tutorials") contains code that does not really work well... I wanted to say about it
     
    adel-akbi likes this.
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you saying that you are pressing the keys at the start of the game, and it takes a few seconds before the ball starts moving?

    Is the scene fully loaded before the ball moves?

    Unity must initialize a lot of information on that first frame.

    To double check, you can pause and play, and then start the game. This way the game will be fully loaded before you start playing.

    To do this, use the play buttons at the top of the editor:
    Screenshot 2015-04-27 22.48.56.png

    First put the editor in pause mode:
    Screenshot 2015-04-27 22.49.53.png

    Then enter playmode:
    Screenshot 2015-04-27 22.50.32.png

    ... and this way, you'll be in paused play-mode. Let the editor fully load and start the scene, then uncheck pause and play the game:
    Screenshot 2015-04-27 22.51.31.png

    The ball should move immediately.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, you are adding force to the ball. The ball will start at a dead stop and as you add force, it will gather speed. This is true of real-world physics. If you have a pool table with one ball on it, and you take a hair dryer or a vacuum cleaner on "blow" (eg: reversed) and you used the air to add force to the ball, it would start slow and accumulate velocity until it reached the maximum speed allowed by air and surface friction.

    The code in the game works fine, and has done for quite a few years. I can't believe it's suddenly changed! Moreover, I just updated it to Unity 5, so it should work fine with the current version of the editor.

    If you still perceive an issue, can you do a video screen capture so we can see what the issue is?

    -

    When it comes to your code, it will start moving faster, as you are applying more force! But only on the x-axis.

    -

    It's also possible, if you were exploring the rigidbody, that you have changed the value of either drag or angular drag? Both of these will act to slow the movement of the rigidbody. Double check that these are at default values.
     
  7. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    I was thinking about ways of expanding this last night, and I think I want to try to make a game like one of those old wooden marble mazes. The ones where you have to tile the board rather than moving the marble. Is there anything special I need to do to the ball to make that work? Such as keeping it from being flung off the board when the board tilts.

    Edit to Add:

    I've started working on a prototype, and got the tilting done. That was the easy part. I may adjust how far or fast it tilts, but for now it is sufficient. The problem I have now is when I tilt it, and the ball is towards one side of the plane, when it resets to the default rotation the ball sometimes falls through it. I was thinking of maybe having it update the balls position so it is always on top of the plane, but I'm not sure if that should go in update, or fixed update. Or if there's a better way to do it. Any advice?
     
    Last edited: Apr 28, 2015
  8. derberherbert

    derberherbert

    Joined:
    Apr 28, 2015
    Posts:
    5
    ctrl + ' takes me to the offline documention, which only gives examples for javascript. How can I convice the offline docs
    to
    (a) give me c# code
    or
    (b) open the online docs, where c# is avaible?
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, physics says if you tilt the base board too much, it will get thrown off! If you simple tilt the base board and let the physics engine take over, it's a possibility. One thing you could do is limit the amount of change that the board can have over any given time. The other thing you could do, would be to cheat, and apply forces, like in rolls a ball, based on the perceived tilt of the board, but don't tilt the board. This would be easier on a phone or tablet, where the baseboard doesn't actually need to be seen to tilt, and the perceived tilt of the device can be enough.

    (Whoops - never posted this and got drawn away - I see you've made progress, so I'll post this as is...)
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Look at the choices of physical collision calculation. You may need to increase the calculations for two dynamic objects (the board and the ball).
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The choice of language is yours. You can decide:
    Screenshot 2015-04-28 10.36.14.png

    When it comes to searching the docs: Local/Online; I'm unclear what the rules are. One is definitely connectivity. If you are not connected to the internet, you get a local search...
     
  12. derberherbert

    derberherbert

    Joined:
    Apr 28, 2015
    Posts:
    5
    that choice does only appear online, offline is only javascript. Solution for now is the delete/rename the offline documentation folder to force an online search.
     
  13. SolidChoice

    SolidChoice

    Joined:
    Apr 28, 2015
    Posts:
    1
    using UnityEngine;
    using System.Collections;

    public class PlayerControler: MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start ()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
    }

    }

    ive don exactly everything that was told in the tutorial yet it always stands "the referenced script on this Behavior is missing" and it dosent even state where the issue is when i check the code. plz assist me and send a reply with eather a solution or a fixed script so it atleast freaking works! ty
     
  14. ua_ACCACIH

    ua_ACCACIH

    Joined:
    Apr 25, 2015
    Posts:
    5
    ok. my version:
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you let me know what version of Unity you are using, what platform and what browser?

    When I test on my machine, I have both languages: Screenshot 2015-04-28 13.07.06.png
     
  16. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    Did you try with a greater speed modifyer ?
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please learn to format your code:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Your script is correct, as far as I can see with a quick glance.

    Don't forget that there are two parts to getting the ball to move: Writing the Script and Setting Up the Scene in the Editor.

    If you have an error when running, then this is probably what's stopping you from playing your game.

    If you need help with an error, please paste the entire error into the post, so we can help you.

    From what you have written: "the referenced script on this Behavior is missing" ...

    ... well ...

    You're missing a script - just like the error says. That's where is stating what the problem is!

    SO: You need to find where the script is missing and replace it.

    As we don't have the entire error, we can't really help you.

    SO: You'll need to post the error here and politely ask for help, or you need to go through all of your GameObjects in the scene's hierarchy and look for the missing script on a component. If you don't know which script should be there, you'll have to go back thru the videos until you find what script was attached to the missing GameObject.

    One way that a script can "go missing", other than you removing it manually, is if you delete the script from your project. This can happen when someone decides to start from scratch, deletes the scripts and starts over. If you've done this, you will need to re-associate that script with the component.

    If you have a missing script, it will something look like this:
    Screenshot 2015-04-28 13.19.59.png
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok. Could be language confusion? And confusion with a bug...

    The Bug: There is a bug in the physics code which we are looking into where the ball, at rest, with no force applied to it starts to slowly roll forward on the z-axis. Sadly, no change in settings will affect this until we ship a fix. This amount of force seems nearly 0, but it's enough for the ball to start rolling if you don't do anything to it.

    Moving the Player: You don't seem to be holding down any of the control keys in the video. To move the ball, you need to give Unity input on the Horizontal and Vertical axes. This is usually done by the ASDW keys or the Arrow keys, where A/D && Left/Right are the Horizontal Axis and W/S && Up/Down are the Vertical Axis.

    The Confusion: You not only need to have the script written correctly (which you have), the script assigned to the GameObject (which you have), the values (in this case speed) set in the inspector (which you have), but you must also give input to the ball using the keyboard.

    Now, if you are using the keyboard, and the Input is not getting to the script, then we have another issue...
     
  20. derberherbert

    derberherbert

    Joined:
    Apr 28, 2015
    Posts:
    5
    Unity 5.01f1 Personal
    MonoDevelop 4.0.1
    Windows 7-64bit
    Firefox 37.0.2
    Chrome 42.0.2311.90 (both browser the same issue) unitydoc.png



    View attachment 136593
     
  21. ua_ACCACIH

    ua_ACCACIH

    Joined:
    Apr 25, 2015
    Posts:
    5
    I do not speak English. You say in the video, but I do not understand English. I use Google Translate and I want to be able to work with Unity.
    I did not know that you can click on the buttons. Run the script and click on the button AWSD. My actions coincide with the actions on your video.
    Thank you for your patience and attention to my request.
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Thanks for the video, as this is what made me understand what you were missing.
     
    ua_ACCACIH likes this.
  23. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    That documentation is antique and really can't have come from the 5.x download.

    I've checked with the relevant dev's and no one has seen such outdated documentation in any of the deployed install packages and no one can imagine how it could be part of the current download.

    Are you saving older versions of Unity? Or some other custom install?

    If you delete that and re-install, you should get the current documentation.
     
  24. derberherbert

    derberherbert

    Joined:
    Apr 28, 2015
    Posts:
    5
    I had an old Unity installation in C:/Program Files (x86)/Unity that I had forgotten about. The 5.x version I installed today refered to that old folder instead of the correct C:/Program Files/Unity folder, where the correct documention is. It all works now, thanks for your help.
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yay! I was wondering about that. I only noticed the full path in the docs file after I posted. As (iirc) on win 7 64 bit, Unity 5 should be in "Program Files" not "Program Files (x86)". The "Program Files" directory is for 64 bit apps, the "Program Files (x86)" is for 32 bit. Of course one can manually change the install location.

    This doesn't explain the issue of not going to online docs...

    With the docs cleaned up on your system, are you going to the Live Docs now?
     
  26. derberherbert

    derberherbert

    Joined:
    Apr 28, 2015
    Posts:
    5
    A book with a question mark in the main program takes me to the offline version in C:/Program Files/ and ctrl + ' on a keyword in mono goes to http://docs.unity3d.com. I tried to replicate the old behaviour by copying the docs to (x86) but the new installation only cares for the live version now.
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I believe the link to the component reference in the manual is expected to be local...
     
  28. Silversmygare

    Silversmygare

    Joined:
    Apr 26, 2015
    Posts:
    2

    I restarted completely and it worked fine! I think I messed up deleting or renaming some scripts in the beginning...

    The only thing, which I've noticed that other people have commented on as well, is that the ball starts moving ever slowly without input. I tried ticking the "Is Kinematic" box instead of gravity and then it stops moving, but now I, the player, can't move it instead! So I went back to "Use Gravity". The plane is perfectly aligned as well, so the problem seems to be another one. But it's a minor problem for now at least, just thought I'd mention it. Keep us updated if you find out what it is! Thanks again :)
     
  29. Freakzilla

    Freakzilla

    Joined:
    Apr 4, 2014
    Posts:
    3
    Hi Adam,

    Firstly, thank you so much for creating this tutorial. Your instructions are some of the best I've come across.

    I've taken this program and added a few things here and there to try and jazz it up a bit. See link below.



    (BTW, anyone is welcome to offer idea/criticisms on the above)

    I am struggling with one thing though, I wanted to to introduce a timer so that I can display how long it took the player to complete the game. I tried to duplicate the "Win Text" UI element but I got a an error "object reference not set to an instance of an object"

    Please see my code below and let me know what you think. all commented out lines are what I did to try and implement the timer and text.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10. //    public Text timerText;
    11.     public AudioSource crunchSound;
    12. //    private float myTimer;
    13.  
    14.  
    15.     private Rigidbody rb;
    16.     private int count;
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.         crunchSound = GetComponent<AudioSource> ();
    21.         count = 0;
    22.         SetCountText ();
    23.         winText.text = "";
    24.         timerText.text = "";
    25.         myTimer = 0;
    26.  
    27.     }
    28.  
    29. //    void Update () {
    30.      
    31. //    myTimer += Time.deltaTime;
    32.      
    33. //    }
    34.  
    35.     void FixedUpdate()
    36.     {
    37.         float moveHorizontal = Input.GetAxis ("Horizontal");
    38.         float moveVertical = Input.GetAxis ("Vertical");
    39.  
    40.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    41.  
    42.         rb.AddForce (movement * speed);
    43.         if (Input.GetKeyDown (KeyCode.Escape))
    44.         {
    45.             Application.Quit ();
    46.         }
    47.  
    48.     }
    49.  
    50.     void OnTriggerEnter(Collider other)
    51.     {
    52.         if (other.gameObject.CompareTag("Pick Up"))
    53.             {
    54.                 other.gameObject.SetActive (false);
    55.                 count = count + 1;
    56.                 SetCountText ();
    57.                 playSound ();
    58.             }
    59.     }
    60.  
    61.     void SetCountText ()
    62.     {
    63.         countText.text = "Count: " + count.ToString ();
    64.         if (count >= 12)
    65.         {
    66.             winText.text = "You Win!";
    67. //            timerText.text = "Your Time" + myTimer;
    68.  
    69.         }
    70.     }
    71.  
    72.     void playSound()
    73.     {
    74.         crunchSound.Play ();
    75.     }
    76.  
    77. }
    78.  
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This slow accumulation of velocity is a new known bug which should be squashed and deployed very soon.
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Cool! I always wanted to add audio to the introduction, but it was going to push the scope beyond the limit for the project. It's great to see you take it on as a stretch goal.

    A couple of things:

    Somewhere in the chain, you've missed a step. You'll need the code defining the Text variable, a GameObject with a UI Text component attached, and the associated reference between the two.

    If you have duplicated the CODE for the WinText with
    Code (csharp):
    1. public Text timerText;
    ... then you will also need to duplicate the WinText GameObject in the scene as well, and then position that UI Element in the scene so it works as a timer in the game.

    If you have already duplicated the WinText GameObject, then make sure you associated it correctly by dragging it into the slot named "Time Text" on the PlayerController component on the Player GameObject.

    In the end, it's the reference that's missing, so you need - ultimately - a UI Text component on a GameObjet associated with the "Time Text" property on the PlayerController component.

    Also: If you have any compile errors, you may not be able to assign this component to a property.

    -

    Now, all that being said, I've seen a couple of things in the code you are working on.

    With this code:
    Code (csharp):
    1.  
    2.         if (count >= 12)
    3.         {
    4.             winText.text = "You Win!";
    5.             timerText.text = "Your Time" + myTimer;
    6.  
    7.         }
    First, a formatting note: This will budge your myTimer value up against the "Your time" string without any gaps to read something like "Your Time1.024345". You should first off, in the string, add any characters you want, like a colon :)) and a space so it will read more like "Your Time: 1.034345". You should also look into how to format the text into a useable format.*

    Are you saying you only want to display the final time when the player is successful? In this setup, it won't display a running total, but will update only when the "YouWin" gets displayed.

    If this is true, you can also dispense with the extra UI Text element, and simple say:

    Code (csharp):
    1.  
    2.         if (count >= 12)
    3.         {
    4.             winText.text = "You Win!\nYour Time: " + myTimer.ToString();
    5.         }
    Here there a couple of things to note:
    • ToString() is optional, as C# will do this automatically if adding a numerical value to a string - unless you use this for your formatting!
    • This will have the same format (typeface, size, color, etc.) as the "You Win" text, because it's in the same block.
    • The text \n will add a new line.**
    A few more points regarding the code:

    Using Update() is perfectly fine for calculating the time passed. This is more important if you are displaying the timer every frame where you want a constantly updating timer. It may be more efficient, however, if you just want overall time, to set a startTime and calculate a total time:
    Code (csharp):
    1. private float startTime;
    2.  
    3.      void Start ()
    4.      {
    5.           startTime = Time.time;
    6.      }
    7.  
    8.     void SetCountText ()
    9.      {
    10.           if (count >= 12)
    11.           {
    12.                float totalTime = Time.time - startTime;
    13.                winText.text = "You Win!\nYour Time: " + totalTime.ToString();
    14.           }
    15.      }
    In this case, startTime will probably always be something like 0.0f, so you may be able to dispense with this step altogether, but it's probably a good habit to get into as you could - say - use startTime to set the time when the first input or force is applied to the Player GameObject.

    The code public AudioSource crunchSound; doesn't need to be, and probably shouldn't be public as you are associating it with the GetComponent in Start(). See this pattern when dealing with the Rigidbody. This GetComponent will run on the first frame of the game and override whatever value you've put in in the inspector. Even though this will probably be the same data, you should code cleanly and not give yourself the chance of making a mind bending error.***

    *Look up String.Format, and the overloads of ToString();
    An example search: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/26etazsy(v=vs.110).aspx#FormatStrings

    ** This is a list of common "escape characters":
    ***Mind bending error: "I've properly associated the Audio component I want in the inspector, but every time I play the game, the reference is wrong and get's the local Audio component on the GameObject, not the one I dragged into the slot in the inspector!!!"
     
  32. mtibo62

    mtibo62

    Joined:
    May 3, 2015
    Posts:
    8
    Whenever, I play the game for me the ball only seems to be sliding along the plane, not rolling. However, once I roll it off and bring it back to the place one it hits the edge of the plane it bounces off and starts to roll.
    This is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     private Rigidbody rb;
    8.  
    9.     void Start ()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         float moveHorizontal = Input.GetAxis ("Horizontal");
    17.         float moveVertical = Input.GetAxis ("Vertical");
    18.  
    19.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.  
    21.         rb.AddForce(movement * speed);
    22.     }
    23. }
    24.  
    and as far as i checked all of the stuff in the components of the player.
    If you need any more info please ask.
     
  33. SupaFaceLess

    SupaFaceLess

    Joined:
    May 5, 2015
    Posts:
    1
    Assets/Scripts/Moving.cs(5,14): error CS0101: The namespace `global::' already contains a definition for `PlayerController' always comes up when I add the script
     
  34. TheMightyGrowlie

    TheMightyGrowlie

    Joined:
    May 5, 2015
    Posts:
    3
    When I use "ctrl + ' " on PC for the Input command as the video says, it takes me to a 404 error instead of searching the term. not sure if this is an error with the current version of MonoDevelop maybe having the wrong URL path for this or not, if I search the term manually it comes up though
     
  35. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you out up a screen shot of your inspector with the player game object selected and make sure we can see all of the components? Also, double check that this is not perceived. The solid color ball might be rotating, but you don't see it. Make sure your transform tools are in local mode, and select the translate tool. This way you can see the translate gizmo and it will rotate along with the ball.
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Huh... Can you tell me things like: OS platform, default browser type and version? I'll look into this, but it's the first I've heard. Are you connected to the web? Do you have multiple versions of unity installed? Have you moved or changed the location of unity or the unity documentation after installing the software?
     
  37. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You may have two scripts called PlayerController. Try searching your project for that name and see if you get two. Also, if you create a script called PlayerController, and the. Rename the script file to, say for example, PlayerController2, the class inside the script will still be PlayerController, and the compiler will complain. If you rename a script, you must rename the class inside the script as well to match.
     
  38. mtibo62

    mtibo62

    Joined:
    May 3, 2015
    Posts:
    8
    unity.PNG
    Also, I'm not exactly sure how to check if it is perceived or select the transform tool -__-
     
  39. TheMightyGrowlie

    TheMightyGrowlie

    Joined:
    May 5, 2015
    Posts:
    3
    I'm on Windows 8 64-bit, default browser is Google Chrome and I didn't have multiple versions of unity installed. it was happening with all the quick searches like it was taking me to the wrong URL so it wasn't searching properly but today it seems to have completely fixed itself without me having restarted my computer or monodevelop or unity since yesterday, so I'm not entirely sure what the problem was
     
  40. Carlos Gherrera

    Carlos Gherrera

    Joined:
    May 7, 2015
    Posts:
    4
    Hello everyone,

    I'm following the tutorials and, in order to learn as much as possible, I decided to mess around, changing some aspects of the tutorial.

    I came up with the idea of slowly modifying the scale of my pickup objects (a loop where I increase their size up to a certain point and then decrease it until they recover their original size...almost like if they were breathing or "¿glowing?")

    In order to do so, I tried the Transform.localScale function but I only managed to make them increase their size forever and ever...

    Could someone help me replicating that desired behaviour please?
     
  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    These are the transform tools:
    Screenshot 2015-05-07 21.39.05.png

    I'd suggest you choose the "translate" tool, which is the one highlighted in that image and looks like 4 directional arrows. The other tools to the right of the "translate" tool are the "rotate", "scale" and "sprite" tools. To the left is the "hand" tool.

    Next, make sure you have the editor set to "local" mode:
    Screenshot 2015-05-07 21.43.23.png

    ... NOT "global" mode:
    Screenshot 2015-05-07 21.44.14.png

    Make sure that tool is selected and try moving the ball by using the ASDW keys, or the arrow keys. If the ball is rolling, you will see the gizmo roll if you are in local mode.
     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Victory!

    See! Problem solved!

    (^_^)

    Post again if you still have issues.
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'd suggest that a simple way to do this would be to affect the Transform.localScale in Update using something like Mathf.PingPong or maybe by affecting the size with Mathf.Sin.
     
  44. mtibo62

    mtibo62

    Joined:
    May 3, 2015
    Posts:
    8
    Thank for letting me know! However, the ball still isn't rolling :(
     
  45. Freakzilla

    Freakzilla

    Joined:
    Apr 4, 2014
    Posts:
    3
    That was the problem! I've sorted it now. I hate even a little bit of clutter, so I'd minimised the components in the inspector. With previous code I can get a timer working on screen - seconds rounded to the nearest integer.

    Initially, yes. Now I'm trying to keep a UI element that shows timer tick up until the user gets all 12 cubes, at which point the "Your time: " text should come on screen along with the winText.

    This is exactly what I did my first time round, for some reason it wouldn't print the whole string so I assumed (bad habit I know) it was either a bug or there was some other requirement and I tried the method submitted in the previous post (creating a new duplicate UI element).

    See the img:


    Please ignore the "timer text", it works, I've just commented out the code for it.

    I've tried several of the escape characters but it doesn't change anything.

    Also, I'm using Update() to show the timer in the top right, how can I stop the timer from counting once all the cubes are "eaten"? Otherwise it looks a little silly when it says "You Win" and the timer is still ticking.

    I know this is pretty long, and beyond the scope of the tutorial. so... thank you so much for your help. :)
     
  46. Carlos Gherrera

    Carlos Gherrera

    Joined:
    May 7, 2015
    Posts:
    4
    Hey Adam, thanks so much for the advice. I managed to get almost the desired behaviour.

    the problem now is that the scale changes way too fast.
    I would like to be able to control the time it takes to go from smallest to biggest in the pingpong effect.

    I tried using Time.deltatime, but it destroyed the scale of my objects

    This is my current code:
    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Scalator : MonoBehaviour {
    7.  
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     void Update () {
    13.         transform.localScale = new Vector3(Mathf.PingPong(Time.time, 0.301F)+0.5F, transform.localScale.y, (Mathf.PingPong(Time.time, 0.301F))+0.5F);
    14.  
    15.  
    16.     }
    17. }
    18.  
    What I tried was to change "Time.time" with "Time.deltatime"...and what happened is what I stated earlier.
    Any ideas on how to fix it?
     
    Last edited: May 10, 2015
  47. TheMightyGrowlie

    TheMightyGrowlie

    Joined:
    May 5, 2015
    Posts:
    3
    I'm on Part 8 of the tutorial where you put in the text and set it to display the score. for some reason when I add the text the canvas defaults it's bottom left corner to the origin and won't let me move it and thus won't allow me to anchor the text to the left corner properly where it can be seen.
     
  48. DazedAndConfused

    DazedAndConfused

    Joined:
    May 8, 2015
    Posts:
    3
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.        
    7.   public float speed = 100.0f;
    8.  
    9.    private Rigidbody rb;
    10.  
    11.             void Start ()
    12.       {
    13.                rb = GetComponent<Rigidbody>();
    14.     }
    15.     void FixedUpdate ()
    16.     {
    17.         float moveHorizontal = Input.GetAxis ("Horizontal");
    18.                float moveVertical = Input.GetAxis ("Vertical");
    19.                    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.  
    21.              rb.AddForce (movement * speed);
    22.             }
    23.     }
    Hey, with this code, the ball doesn't move. Thank you for being so awesome with help, it is very refreshing compared to other sites. I'm pretty new to this, I do have the speed set to 500 and the position does not change. Sorry, thank you, any help is appreciated. There are no errors, it just doesn't move
     
  49. Carlos Gherrera

    Carlos Gherrera

    Joined:
    May 7, 2015
    Posts:
    4
    I am not any sort of expert, but I think that, when you declare a variable as "public", Unity expects you to mofidy that variable in the actual software.

    Try removing the "= 100.f" from your "public float speed" and change the speed inside Unity instead...that's the only thing your code has different from mine, and mine works.
     
  50. DazedAndConfused

    DazedAndConfused

    Joined:
    May 8, 2015
    Posts:
    3
    No, that still does not work, the script and rigidbody are both under the player inspector and the speed is set to 500. Is there anyway it is not reading the script? How can I tell?