Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D UFO Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Matthew-Schell, Jan 22, 2016.

  1. MrJayNash

    MrJayNash

    Joined:
    Jul 31, 2016
    Posts:
    2
    Hello, i am fairly new to programming and making games (obviously), so i thought i should try out your tutorial, and it is really helpful! Thank you so much.

    However, i have a strange problem. When i mak a build of my scene, so to speak "create a gamefile", in the game the Player/UFO disappears and reappears randomly after collecting the PickUps. Interestingly, at what point it appears and disappears seems to vary from build to build. However, everything seems to be fine in the editor and if i build the sample Scene instead of my own.
    The code is definetely correct, i checked multiple times and even inserted the complete code you provided.
    I have Unity personal edition 5.3.5f1
    I also attached an image of my settings of the player object. Like i said, my code is currently the one you provided.

    I would be really grateful if someone could help me. I 'd like to progress further and create more games, but i just hate such bugs appearing and cant do anything about them ;)

    Cheers, Jay
     

    Attached Files:

  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon,

    it looks like you have forgot to assign sorting layers to your sprites.

    best to ensure that you have assigned everything into the correct layers, have another look through this vid and double check.
    http://unity3d.com/learn/tutorials/projects/2d-ufo-tutorial/setting-play-field?playlist=25844
     
  3. MrJayNash

    MrJayNash

    Joined:
    Jul 31, 2016
    Posts:
    2
    Oh yes! oh my gosh, you are totally right! I assigned the layer for the pickup but completely forgot to do the same for the Player, haha. Thank you so much for your help.
    Now, off to find other good tutorials to carry on from here ^^

    Have a great day!
     
    OboShape likes this.
  4. WarriusBirde

    WarriusBirde

    Joined:
    Jul 28, 2016
    Posts:
    4
    That was it. I'm not sure how it got set to however it was, but unticking the canvas gizmo removed the issue. Thanks!
     
  5. JunnyQuan

    JunnyQuan

    Joined:
    Jul 31, 2016
    Posts:
    1
    Does anyone face the same prob? After I have build the game,my player gone invisible... However i'm still able to move my player and collect items and also my player will appear sometimes but missing most of the time.
     

    Attached Files:

  6. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you double checked that you player is on the correct layer (the player sorting layer), as if there is more than one thing on the default layer, drawing sequence isnt always the same, so things might get drawn in a different order and therefore can be obscured.
     
  7. ManiSam

    ManiSam

    Joined:
    Aug 22, 2016
    Posts:
    2
    Hi guys - I am at video 4- Adding Collision. For some reason, the player object does not wobble around on its own like how it does in the video. As a result, it is just stuck in the center. I had to add the gravity scale to check if the collision setup worked. It just hits the bottom wall after adding gravity scale and is stuck there. It does not bounce off of it. Any help much appreciated.
     
  8. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    were you able to move the player around during the 'Controlling the Player' video?
    just curious as you say it is stuck in the middle.
     
  9. ManiSam

    ManiSam

    Joined:
    Aug 22, 2016
    Posts:
    2
    Thank you. Didn't realize that video 3 enabled movements of the player with the arrow keys. It is working fine. Absolute beginner here!!
     
    OboShape likes this.
  10. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    nice one, yea best to follow in sequence to get most from it.
     
  11. bengaard

    bengaard

    Joined:
    Aug 24, 2016
    Posts:
    1
    Hi, thanks for a great tutorial, works fine on windows.
    Now I would like to export it to android but I can't make the ufo move.
    I try like this:

    void FixedUpdate()
    {
    float moveHorizontal = Input.acceleration.x;
    float moveVertical = Input.acceleration.y;
    Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    rb2d.AddForce (movement * speed);
    }

    but nothing happens on the phone. Maybe it isn't so simple?
     
  12. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    not done much at all with mobile, but...

    Might be worth while having a look through the mobile teaching section
    https://unity3d.com/learn/tutorials/topics/mobile-touch

    also have a look at some of the live training sessions related to mobile too.
    although related to converting the space shooter to mobile, there will definitely be some good info for using the accellerometer in it.
    https://unity3d.com/learn/tutorials...e-development-converting-space-shooter-mobile
     
  13. Ectoplasmictoast

    Ectoplasmictoast

    Joined:
    Sep 11, 2016
    Posts:
    1
    Hello! I am currently following the tutorial with very little problems. I turned the gravity to 0 after it testing correctly with 1 gravity on and I have an error message saying "The referenced script on this Behavior is missing!" I have the script attached as a component for the UFO here is my code
     

    Attached Files:

  14. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,
    Looking at that screenshot.
    Your filename of the script is 'Playercontroller' and within the script the class name is 'CompletePlayerController'

    These must match so you have to change the class name to be the same as the filename, it's a Unity thing for the scripts to work.

    Once you have done that try removing and readding the script and test.
     
  15. umraz

    umraz

    Joined:
    Sep 23, 2016
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     private Rigidbody2D rb2d;
    7.  
    8.     void Start ()
    9.     {
    10.         rb2d = GetComponent<Rigidbody2D> ();
    11.     }
    12.  
    13.     void FixedUpdate ()
    14.     {
    15.         float moveHorizontal = Input.GetAxis ("Horizontal");
    16.         float moveVertical = Input.GetAxis ("Vertical");
    17.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    18.         rb2d.AddForce (movement);
    19.     }
    20. }
    21.  
    hi , i can move the player but it only moves left and right , it dosent move up and down and idea how do i fix it ?
     
  16. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    can you have a look at the rigidbody on the player, theres a little dropdown arrow for Constraints, see if theres any ticks in there that may limit its movement.
     
  17. KviK4iK

    KviK4iK

    Joined:
    Sep 29, 2016
    Posts:
    1
    Помочь с этой проблемой, я искал в интернете, но не смог найти. Я буду очень признателен.
    -------------------------------------------------- -------------------------------------------------- -------------------------------------------------
    Ошибка CS1061: (? Вы пропали без вести с помощью директивы или ссылка на сборку) Тип `UnityEngine.Renderer 'не содержит определение для` IsVisibleFrom' и без метода расширения` IsVisibleFrom 'типа` UnityEngine.Renderer' может быть найден
    -------------------------------------------------- -------------------------------------------------- -------------------------------------------------
    используя System.Collections.Generic;
    используя System.Linq;
    используя UnityEngine;


    RendererExtensions Открытый класс: MonoBehaviour
    {

    общественная скорость Vector2 = новый Vector2 (10, 10);


    общественное направление Vector2 = новый Vector2 (-1, 0);


    BOOL isLinkedToCamera общественности = ложь;


    BOOL isLooping общественности = ложь;


    частный List <Transform> backgroundPart;


    аннулированию Start ()
    {

    если (isLooping)
    {

    backgroundPart = новый List <Transform> ();

    для (INT I = 0; я <transform.childCount; я ++)
    {
    Transform ребенка = transform.GetChild (я);


    если (child.GetComponent <видеообработки> ()! = NULL)
    {
    backgroundPart.Add (ребенок);
    }
    }


    backgroundPart = backgroundPart.OrderBy (
    т => t.position.x
    ).К списку();
    }
    }

    аннулированию Update ()
    {

    Vector3 движение = новый Vector3 (
    speed.x * direction.x,
    speed.y * direction.y,
    0);

    движение * = Time.deltaTime;
    transform.Translate (движение);


    если (isLinkedToCamera)
    {
    Camera.main.transform.Translate (движение);
    }


    если (isLooping)
    {

    Transform FirstChild = backgroundPart.FirstOrDefault ();

    если (FirstChild! = NULL)
    {

    если (firstChild.position.x <Camera.main.transform.position.x)
    {

    если (firstChild.GetComponent <видеообработки> (). IsVisibleFrom (Camera.main) == ложь) /// ОШИБКА
    {

    Transform LastChild = backgroundPart.LastOrDefault ();
    Vector3 lastPosition = lastChild.transform.position;
    Vector3 lastSize = (lastChild.GetComponent <видеообработки> () bounds.max - lastChild.GetComponent <видеообработки> () bounds.min..);


    firstChild.position = новый Vector3 (lastPosition.x + lastSize.x, firstChild.position.y, firstChild.position.z);


    backgroundPart.Remove (FirstChild);
    backgroundPart.Add (FirstChild);
    }
    }
    }
    }
    }
    }
     
  18. Deleted User

    Deleted User

    Guest

    Hi,

    Just posting here to say that I just made this tutorial entirely with Unity 5.5.0b6 and that I didn't experience any problem at all.

    I just decided not to use the script attached to the camera because having the camera following the player in this case is not adequate, in my opinion.

    Great job! :)
     
    Matthew-Schell and OboShape like this.
  19. Deleted User

    Deleted User

    Guest

    I just finished the 'Adding Collisions' video, but in my project, the UFO isn't colliding with any of the walls. Instead, when I move the UFO, is just slides off the background as if there were no colliders. Does anyone know what I could be doing wrong? I went back and checked that my script and components match the tutorial's, and everything seems to be fine there.

    Edit: It works now. I accidentally had 'Is Trigger' checked in the Circle Collider 2D component. Unchecking it solved the problem.
     
  20. joeeydubu

    joeeydubu

    Joined:
    Dec 14, 2016
    Posts:
    2
    {
    I don't get the pop ups for the reference to put the player in? at 6:45? please help
    }
     
  21. joeeydubu

    joeeydubu

    Joined:
    Dec 14, 2016
    Posts:
    2
    its not a code problem either because I typed it all and it never popped up, I even copied the code directly and its still not popping up?
     
  22. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Make sure you have the camera selected, not the player object, then in the inspector you should see the camera controller script. There should be a space for the player object.

    If not then make sure you have the GameObject player set to public, not private.
     
    Matthew-Schell likes this.
  23. Fraiyn

    Fraiyn

    Joined:
    Jan 5, 2017
    Posts:
    2
    I encountered a weird problem. All of my tags are correct, and I can't seem to find any problems. If I run the game in game window, everything works like they should. But after I build the game and start it, a curious thing happens.

    When I move my ufo to pickups, the count goes up, but pickups doesn't disappear. So, the player object obviously reacts when colliding with the pickups, but they're not set to false in SetActive. You win text appears normally too, so count works as well. Also, you can get points from the same pickups over and over again.

    Any ideas what might be causing this? The script for player object seems to be alright and I even copypasted the example text to test. Still, this problem occurs in built game, but not in game window itself.

    Here's the code for the pickup SetActive part:

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.CompareTag("PickUp"))

    other.gameObject.SetActive(false);

    count = count + 1;

    SetCountText ();
    }

    Thanks in advance.
     
  24. Deleted User

    Deleted User

    Guest

  25. Fraiyn

    Fraiyn

    Joined:
    Jan 5, 2017
    Posts:
    2
    Oh, I was wondering how that worked. Thanks. Unfortunately, seems my whole project has been corrupted somehow and Unity crashes when it tries to open the file. So I'll post the script later. =/
     
  26. c-desmarais

    c-desmarais

    Joined:
    Feb 3, 2017
    Posts:
    1
    Hi,
    My sprites don't seem to be showing up correctly in both the scene view and the game view. We can only see the outline of the sprite. However, in the camera preview, it shows up correctly. Any idea what I am doing wrong?

    Thanks so much :)
     

    Attached Files:

  27. Matthew-Schell

    Matthew-Schell

    Joined:
    Oct 1, 2014
    Posts:
    238

    Please make sure your Scene view is in 2D mode by clicking the 2D button at the top of the Scene view, see image linked here:

    https://gyazo.com/f4e9aa27341aae6003ab4d1dcf1e0176?token=4a71157b6dbcbf92841e42750d9a9fb5
     
  28. ttorquati

    ttorquati

    Joined:
    Feb 10, 2017
    Posts:
    2
    Hello,

    Thank you for the nice tutorial, Since Im new to Unity Programming I was able to understand almost everything. I've also completed the tutorial and the game is working properly.

    Now I am trying to work on new features to learn more so I am not sure if this is the right place to make the following questions.

    Feature v1 - Spawn Gold Pickups at Random Locations

    I was able to make this work but not entirely correct. Somehow I found that the board to spawn has the range of positions from -11 to 11 for both X and Y directions.

    However, when trying to use Random.Range, it seems to select only values from 0-11 for both X and Y, making the gold pickups appearing only in the superior right side of the Board.

    I've also tried to use the BoardManager Script from the 2D Rogue Like Tutorial, but found the same problem at the end.

    I also tried to move the Background to the superior right location so that way it will only use positive values like from 0-22 instead of -11 to 11 but Im not sure if it is right to do that.

    Do you have any idea how to make this feature?

    Feature v2 - Do not Spawn Gold Pickups when another Gold Pickup already exists in the place

    Using the 2D Rogue Like BoardManager Script I found that they use a Vector3 GridPosition to make sure to not spawn units/items in positions already being used. Using the gridposition and RemoveAt.

    The problem is that in this UFO project is not working, it keeps placing gold pickups together sometimes. Not sure how the grid works, how to specify the grid size, if the board of UFO game is also 8x8.

    Well, those are my question at moment, Ill keep trying to find the solution...

    ty all gl hf
     
  29. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Random.Range should go into negative values.

    From the scripting API.

    public GameObject prefab;

    // Instantiate the prefab somewhere between -10.0 and 10.0 on the x-z plane
    void Start()
    {
    Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
    Instantiate(prefab, position, Quaternion.identity);
    }
     
    ttorquati and Matthew-Schell like this.
  30. Matthew-Schell

    Matthew-Schell

    Joined:
    Oct 1, 2014
    Posts:
    238
    Yes, Ryeath is correct, you can spawn from -11 to 11 by calling Random.Range(-11,11);

    With regard to not overlapping, you'll want to do some kind of check to see if the space is filled before spawning.

    I might start by looking at Physics2D.OverlapCircle:

    https://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html

    The basics would be:

    1) Pick a random location
    2) Call OverlapCircle at that location
    3) If OverlapCircle didn't return a collider from that location
    4) Spawn your object
     
    ttorquati likes this.
  31. ttorquati

    ttorquati

    Joined:
    Feb 10, 2017
    Posts:
    2
    Ty so much Ryeath and Matthew-Schell... Could make both to work, you guys are awesome...

    Here is the code I've used just for information

    Code (csharp):
    1.  
    2. void spawnAtRandomPosition() {
    3.   bool notAvailable = true;
    4.   while (notAvailable) {
    5.     float x = Random.Range(xMin, xMax);
    6.     float y = Random.Range(yMin, yMax);
    7.  
    8.     if (!Physics2D.OverlapCircle(new Vector2(x, y), 2f)) {
    9.       Instantiate(goldPickUp, new Vector2(x, y), Quaternion.identity);
    10.       notAvailable = false;
    11.     }
    12.   }              
    13. }
    14.  
     
    Matthew-Schell likes this.
  32. tccul0

    tccul0

    Joined:
    Feb 14, 2017
    Posts:
    1
    Hi all, I have a probably silly question, but when I try and follow along with the video tutorials, what I see is nothing like what the video shows. I have included a screenshot. The asset package I download from the store contains a completed project, which I don't want. The list of assets that I have the option to import is much bigger in size than that in the video, and when I try to go through it one-by-one to select only the assets that are in the video, I get errors as I move on to other videos because I have extra assets that I shouldn't have, or I am missing some that I need but don't have. Please help!

    Thanks a bunch!
     

    Attached Files:

  33. conward1

    conward1

    Joined:
    Feb 16, 2017
    Posts:
    1
    Thank you! This fixed my problem.
     
  34. nijagu

    nijagu

    Joined:
    Feb 12, 2017
    Posts:
    8
    Compare void start() and void Start()
     
  35. RyaanH

    RyaanH

    Joined:
    Apr 2, 2017
    Posts:
    3
    Screen Shot 2017-04-02 at 4.53.58 PM.png
    Why is that my player doesn't move at all when I have this script?
     
  36. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Script looks ok RyaanX.

    Make sure your script is attached as a component to your player object and make sure you enter a value for speed in your inspector. IF that is all good check your input manager to see if anything is strange.
     
  37. RyaanH

    RyaanH

    Joined:
    Apr 2, 2017
    Posts:
    3
    I have it as a component and I do add a speed value.

    How do I check the input manager?
    If that is the debugger, I have no errors. I am confused to why it doesn't work.
     
  38. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    In the top menu Edit>Project Settings>Input.

    Check the input setting for horizontal and vertical to make sure the keys are correct.

    Also make sure on your Rigidbody2D on your player that isKinnematic is not selected.
     
  39. RyaanH

    RyaanH

    Joined:
    Apr 2, 2017
    Posts:
    3
    Thanks, I got it to work.
     
  40. bronygamer44

    bronygamer44

    Joined:
    Apr 7, 2017
    Posts:
    1
    By any chance, if "if trigger is off, does it bump into pickups and then collect them?
     
  41. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning.
    Sorry didnt notice earlier
    Check where you have
    onTriggerEnter2D

    Missing the capital 'O' at the start
    OnTriggerEnter2D
     
  42. BProkz

    BProkz

    Joined:
    May 16, 2017
    Posts:
    2
    I'm having an issue where when i attach the camera to the player object, i abruptly lose everything in my Camera preview (All i see is background colour of sky-box) and when i go to play, all i see is that as one solid colour. I've checked the positioning and size of the camera, and i've followed this step of the tutorial through 2-3 times over and i still cant see where i've gone wrong (Option wise or otherwise) if you could please point out a solution or lead me in the right direction that would be wonderful please and thanks. All of the instructions have been crystal clear and have worked perfectly up until this point.

    I read through the replies on the other pages and saw that someone else had this problem and they suggested the size was negative, but i checked and i've tried changing the values from its 16.5 from previous video to 25, and -25 to see if i could see anything but the camera preview is still grey as well as when i press Play.

    Thank you for the help.



    Player Object Screenshot


    Update: I realize the tutorial has you undo this step. However during the step my camera does not attach to the object properly, however i have finished the video 5 & 6 now without any further problems.
     

    Attached Files:

    Last edited: May 16, 2017
  43. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you resolved your earlier issues?, were you able to continue with the series ok?
     
  44. BProkz

    BProkz

    Joined:
    May 16, 2017
    Posts:
    2
    I was able to move passed this step as the next step after testing is to undo it and then make a script for camera controls, so you don't need to have the camera attached to the parent object. I'm wondering if its because of a difference in version in unity for the behavior. I was unable to do that portion of the video/step with the play testing of camera attached as child of the player object, so i passed on by as opposed to being able to resolve the particular issue, because as i said the next step in the video was to undo it.

    Thanks for the quick reply.
     
  45. ESJ2000

    ESJ2000

    Joined:
    May 22, 2017
    Posts:
    1
    My UFO won't collect the nuggets, does anyone have any idea why?



    using UnityEngine;
    using System.Collections;

    public class CompletePlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody2D rb2d;

    void Start()
    {
    rb2d = GetComponent<Rigidbody2D> ();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    rb2d.AddForce (movement * speed);
    }


    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    }
    }
    }
     
  46. yugashini

    yugashini

    Joined:
    Jun 8, 2017
    Posts:
    1
    hello can u provide me the swipe coding for this game .. when the apk file is put in the android device there is no option to move so the only option is to swipe so i need the coding for that .. can u help me ..
     
  47. JoniBerg

    JoniBerg

    Joined:
    Jun 30, 2017
    Posts:
    14
    Hi I working 4 periods on this UFO tutorial.

    It is nice tutorial but my player Ufo ball moving only top down. Colliders working and player is on area.

    Helped me please. Why my player not moving around? Only top down. :(

    My Player Controller Script:

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {


    public float speed;
    private Rigidbody2D rb2d;

    void Start()
    {
    rb2d = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    rb2d.AddForce (movement * speed);
    }

    }

    Why this ^^ not working? Thank you for helped me. :) Sorry my english is so bad... hopefully you understand....
     
    KAYUMIY likes this.
  48. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    From tutorial "We will move the UFO using physics and forces".
    Please can you show me 2 examples for physics and forces?
    When do we use physics to move the object?
    When do we use forces to move the object?
    What is the difference between physics and forces?
     
  49. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115

    Your code is correct. Please check the Player's Inspector panel. It should be same with the picture I sent. sda.jpg

    If it does not move horizontally, please try from scratch.
     
  50. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Please add "Debug.Log("Collision detected")" code in the "OnTriggerEnter2D" function. And check one more time.
    If there is not "Collision detected" word in the console.
    Check the both game objects' Insperctor Panel, especially, Rigidbody and Colliders.