Search Unity

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

Official Ruby’s Adventure: 2d Beginner Official Thread

Discussion in 'Community Learning & Teaching' started by Woolley_Kat, Apr 15, 2019.

  1. AbbyStrawby

    AbbyStrawby

    Joined:
    May 31, 2019
    Posts:
    14
    How do you go about Instantiating particle effects as said in the last part of the particle effects lesson, ive stored a reference to the effect prefab in the health pickup code but i dont know how to instantiate it properly, the effect doesnt display when i simply say:

    Instantiate(PickupEffect);

    inside the collect health function. So am I formatting instantiate wrong or is something else the issue? ive already checked that the effect in the scene isnt in the negative z axis and made sure that the health pickup effect stored in the code is the child of the collectible.
     
  2. Bentobbs

    Bentobbs

    Joined:
    Jun 11, 2019
    Posts:
    2
    That fixed it. Great! Thanks a lot!
     
  3. tasnimrushi10

    tasnimrushi10

    Joined:
    Jun 23, 2019
    Posts:
    8

    I need a help with Launching the projectile part. I'm not getting these lines.
    "Let’s start by creating a public variable in our RubyController script that we will call projectilePrefab. We’ll make it a GameObject type because that’s what prefabs are; GameObjects saved as assets. Because we’ve made it public, it appears in our Editor as a slot where we can assign any GameObject. Drag and drop the projectile prefab into that slot. Remember that if you did that in the Scene, it will be an override to your prefab (with a blue line next to the entry), so use the Overrides drop-down in the top right corner to apply them to the prefab."

    Code (CSharp):
    1. public GameObject projectilePrefab;
    I've declared a veriable like this. Is that what the lines says? If so then Where is the GameObject where I can drag and drop the projectile prefab. Also is CogBullet prefab is the projectile prefab? and where is the editor window?

    Regards,
    Tasnim
     
  4. tasnimrushi10

    tasnimrushi10

    Joined:
    Jun 23, 2019
    Posts:
    8
    I'm stucked at part 10(2). When I wrote the code:
    Code (CSharp):
    1.     void Launch()
    2.     {
    3.         GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
    4.         Projectile projectile = projectileObject.GetComponent<Projectile>();
    5.         projectile.Launch(lookDirection, 300);
    6.  
    7.         animator.SetTrigger("Launch");
    8.  
    9.     }
    but it shows the following error: Assets\Scripts\RubyController.cs(80,51): error CS0103: The name 'projectilePrefab' does not exist in the current context

    how can I fix this

    Also if I set
    Code (CSharp):
    1. public GameObject projectilePrefab;
    the error disappears. But where is the projectile prefab on the editor window to drag and drop projectile prefab there?
     
    Last edited: Jun 28, 2019
  5. GregKelly7

    GregKelly7

    Joined:
    Jun 26, 2019
    Posts:
    6
    This post helped me quite a lot, thanks :)
     
  6. GregKelly7

    GregKelly7

    Joined:
    Jun 26, 2019
    Posts:
    6
    Thanks this helped :)
     
  7. CarMarc

    CarMarc

    Joined:
    Apr 28, 2019
    Posts:
    1
    I'm facing exactly the same problem!

    Any help with this would be very much welcome.
     
  8. qibyte

    qibyte

    Joined:
    Jun 25, 2019
    Posts:
    4
    That's true. I fixed it as suggested. Thanks.
     
  9. AbbyStrawby

    AbbyStrawby

    Joined:
    May 31, 2019
    Posts:
    14
    Hi, its been a few days and I still can't figure out how to instantiate my particle effect for the health pickup. Can anyone help with this?
     
  10. AbbyStrawby

    AbbyStrawby

    Joined:
    May 31, 2019
    Posts:
    14
    Ok so I'm just real dumb huh. I was thinking that the health pickup effect needed to be a child of the collectible health item, like how the robot had the smoke, but that isnt true because if you have it there it will play immediately upon starting the game, and we dont want that which is why we instantiate it on pickup. My second problem is that for some reason you needed a rigidbody to get the position of an item which just isnt true (just use transform.position) and now it appears on the strawberry. I'll add my code below for anyone else who was struggling with it. Make sure play on awake is true on your particle

    public class HealthCollectible : MonoBehaviour
    {
    public ParticleSystem PickupEffect;

    void OnTriggerEnter2D(Collider2D other)
    {
    RubyController controller = other.GetComponent<RubyController>();

    if (controller != null)
    {
    if (controller.health < controller.maxHealth)
    {
    controller.ChangeHealth(1);
    Instantiate(PickupEffect, transform.position, Quaternion.identity);
    PickupEffect.Play();
    Destroy(gameObject);
    }
    }
    }
    }
     
  11. computergoddess

    computergoddess

    Joined:
    Sep 29, 2016
    Posts:
    6
    Just change those 2 lines? (I am one of those beginners.)Okay but in the Animator we set it up to Move X and Move Y so how do you solve that or does it matter?
     
  12. GregKelly7

    GregKelly7

    Joined:
    Jun 26, 2019
    Posts:
    6
    EDIT - NO NEED TO REPLY TO THIS, I FOUND THE ANSWER (see below)

    https://answers.unity.com/questions/871238/cant-change-the-pivot-of-a-ui-element.html

    Hello again,

    I am having trouble with Lesson 13 Visual Styling - UI - Head-Up Display: Part 4 (Masking the Health Bar).

    On a Mac, I'm unable to select then move the pivot point.

    The steps are:
    1. Create an Image GameObject child of your Health GameObject (call it Mask)
    2. Resize it
    3. Move its anchors to fit the empty place in your UI where the Health bar goes

    The problem I am having is with the 4th step: Move the pivot (of Mask) to the left side.

    Answer: You need to switch from Center to Pivot in the toggle just below the menu:

    p1.png
     
    Last edited: Jul 3, 2019
    spuddy93 and lastlittlebird like this.
  13. GregKelly7

    GregKelly7

    Joined:
    Jun 26, 2019
    Posts:
    6
    I think this is what you need to do:

    1. Select "Ruby" in the Hierarchy tree (so that she appears in Inspector)
    2. In the Inspector, you will see an empty box called Projectile Prefab (this should be underneath Ruby Controller Script)
    3. In the Project - Assets - Prefab folder: find "CogBullet"
    4. Drag "CogBullet" into the empty box in step 2

    See my screenshot below:

    Projectile Prefab.png

    Cheers

    Greg
     
  14. MilouMilou

    MilouMilou

    Joined:
    Jul 3, 2019
    Posts:
    2
    Chapter 10:
    My projectile isn't moving. Upon pressing (c) the projectile appears but that's it. When I press (c) again, it disappears. Where did I go wrong?

    Script:

    [RUBY CONTROLLER SCRIPT]

    public class RubyController : MonoBehaviour
    {
    public float speed = 3.0f;
    public int maxHealth = 5;
    public float timeInvincible = 2.0f;
    public int currentHealth;
    bool isInvincible;
    float invincibleTimer;
    Rigidbody2D rigidbody2d;
    Animator animator;
    Vector2 lookDirection = new Vector2(1, 0);
    public GameObject projectilePrefab;
    // Start is called before the first frame update
    void Awake()
    {
    rigidbody2d = GetComponent<Rigidbody2D>();
    currentHealth = maxHealth;
    animator = GetComponent<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");
    Vector2 move = new Vector2(horizontal, vertical);
    if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    {
    lookDirection.Set(move.x, move.y);
    lookDirection.Normalize();
    }
    animator.SetFloat("Look X", lookDirection.x);
    animator.SetFloat("Look Y", lookDirection.y);
    animator.SetFloat("Speed", move.magnitude);
    Vector2 position = rigidbody2d.position;
    position = position + move * speed * Time.deltaTime;
    rigidbody2d.MovePosition(position);
    if (isInvincible)
    {
    invincibleTimer -= Time.deltaTime;
    if (invincibleTimer < 0)
    isInvincible = false;
    }
    if (Input.GetKeyDown(KeyCode.C))
    {
    Launch();
    }
    }
    public void ChangeHealth(int amount)
    {
    if (amount < 0)
    {
    if (isInvincible)
    return;
    isInvincible = true;
    invincibleTimer = timeInvincible;
    }
    currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    Debug.Log(currentHealth + "/" + maxHealth);
    }
    void Launch()
    {
    GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
    Debug.Log("I'm moving");
    Projectile projectile = projectileObject.GetComponent<Projectile>();
    projectile.Launch(lookDirection, 300);
    animator.SetTrigger("Launch");
    }

    }

    [PROJECTILE SCRIPT]
    public class Projectile : MonoBehaviour
    {
    Rigidbody2D rigidbody2d;
    Animator animator;
    void Start()
    {
    rigidbody2d = GetComponent<Rigidbody2D>();
    animator = GetComponent<Animator>();
    }
    public void Launch(Vector2 direction, float force)
    {
    rigidbody2d.AddForce(direction * force);
    }
    void OnCollisionEnter2D(Collision2D other)
    {
    //EnemyController e = other.collider.GetComponent<EnemyController>();
    //if (e != null)
    //{
    // e.Fix();
    // }
    Destroy(gameObject);
    }

    }
     
  15. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    See tutorial - World Interactions - Projectile

    Step 2. Launching the projectile

    Carry on from this part in tutorial.

    Also, in your projectile script, you have added an Animator variable, which is not required.
     
    tjamz likes this.
  16. MilouMilou

    MilouMilou

    Joined:
    Jul 3, 2019
    Posts:
    2
    [Edit] Found it.. I changed void Start() to void Awake() in the wrong script (in Ruby instead of the projectiler..)

    Ok thank you! I'll try. I'm beyond step 2 though, it's not colliding with Ruby, it doesn't seem to collide with anything at all. But I guess I'll just do it all over again. :)

    Oh yes that animator variable :D never mind it.
     
    Last edited: Jul 3, 2019
    frankcastillo837 likes this.
  17. GregKelly7

    GregKelly7

    Joined:
    Jun 26, 2019
    Posts:
    6
    Thank you to the Unity team for creating this tutorial. It was a lot of fun. As someone with no programing experience, I made it through to the end.
     
  18. JohanAhlqvist

    JohanAhlqvist

    Joined:
    Jun 28, 2019
    Posts:
    3
    Im having some trouble in world interactions - Damage zones and enemies. Particularly the part where you script the enemy to move and damage the player. After a couple of hits from the enemy the controls stop working correctly and no matter what I push the player character gets "dragged" away from the enemy.

    Ive been going through the code(which is the same as in the tutorial as I just write what they tell me to) and cant really find anything wrong with it(but I will include rubyController and enemyController scripts below anyway). I now have two thoughts on what could be wrong. Number one is that the rigidbody2d component is somehow configured wrong, I might have done something weird and number two is that there has been some change from 2018.3 to 2019.1.8f1 which I am using that creates this weird bug. What do you think?

    edit: As I suspected something with the rigidbody2d component I decided to add it to one of my boxes in the scene. So I added the component and configured it and started the "game" to test it out. I pushed around the box for a while and no sight of the bug. So it has to be the script, I must have written something wrong in the script.

    edit2: I now decided to push the box in front of the enemy to see if I could replicate the bug and it did replicate. After a few pushes from the enemy the box started to float away.

    RubyController script:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class RubyController : MonoBehaviour
    7. {
    8.     public int maxHealth = 5;
    9.     public float timeInvincible = 2.0f;
    10.  
    11.     public int health { get { return currentHealth; } }
    12.     int currentHealth;
    13.     bool isInvincible;
    14.     float invincibleTimer;
    15.     public float characterSpeed = 3.0f;
    16.  
    17.  
    18.     Rigidbody2D rigidbody2d;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         rigidbody2d = GetComponent<Rigidbody2D>();
    24.         currentHealth = maxHealth;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         float horizontal = Input.GetAxis("Horizontal");
    31.         float vertical = Input.GetAxis("Vertical");
    32.         Vector2 position = rigidbody2d.position;
    33.         position.x = position.x + characterSpeed * horizontal * Time.deltaTime;
    34.         position.y = position.y + characterSpeed * vertical * Time.deltaTime;
    35.         transform.position = position;
    36.  
    37.         rigidbody2d.MovePosition(position);
    38.  
    39.         if (isInvincible)
    40.         {
    41.             invincibleTimer -= Time.deltaTime;
    42.             if (invincibleTimer < 0)
    43.                 isInvincible = false;
    44.         }
    45.     }
    46.  
    47.     public void ChangeHealth(int amount)
    48.     {
    49.         if (amount < 0)
    50.         {
    51.             if (isInvincible)
    52.                 return;
    53.  
    54.             isInvincible = true;
    55.             invincibleTimer = timeInvincible;
    56.         }
    57.  
    58.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    59.  
    60.         Debug.Log(currentHealth + "/" + maxHealth);
    61.     }
    62. }
    63.  
    EnemyController script:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EnemyController : MonoBehaviour
    7. {
    8.     public float enemySpeed = 3.0f;
    9.     public bool vertical;
    10.     public float changeTime = 3.0f;
    11.  
    12.     Rigidbody2D rigidbody2D;
    13.     float timer;
    14.     int direction = 1;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         rigidbody2D = GetComponent<Rigidbody2D>();
    20.         timer = changeTime;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         timer -= Time.deltaTime;
    27.  
    28.         if (timer < 0)
    29.         {
    30.             direction = -direction;
    31.             timer = changeTime;
    32.         }
    33.  
    34.         Vector2 position = rigidbody2D.position;
    35.      
    36.         if (vertical)
    37.         {
    38.             position.y = position.y + Time.deltaTime * enemySpeed * direction;
    39.         }
    40.  
    41.         else
    42.         {
    43.             position.x = position.x + Time.deltaTime * enemySpeed * direction;
    44.         }
    45.  
    46.         rigidbody2D.MovePosition(position);
    47.     }
    48.  
    49.     void OnCollisionEnter2D(Collision2D other)
    50.     {
    51.         RubyController player = other.gameObject.GetComponent<RubyController >();
    52.  
    53.         if (player != null)
    54.        {
    55.             player.ChangeHealth(-1);
    56.         }
    57.     }
    58. }
    59.  
     
    Last edited: Jul 4, 2019
  19. Exceeds

    Exceeds

    Joined:
    May 26, 2017
    Posts:
    2
    I have found a case where if you quickly press the key to move, in some cases the lookDirection Vector sets to (0,0) and because of that the projectile gets zero speed, I dont understand why this happens.

    Nvm it is because sometimes the input is too small for .Normalize() so it sets to zero.
     
    Last edited: Jul 13, 2019
  20. bridgetkacprzyk174

    bridgetkacprzyk174

    Joined:
    Jul 5, 2019
    Posts:
    5
    I'm having trouble with Ruby walking on objects and the Box Collider not working properly. I put the box collider on an objects and ruby can still walk through it. Also random spaces I cant walk through have appeared. I've followed the tutorial exactly and I've gone back through it at least 5 times. I'm completely lost. I'm not sure if there's a way i can share my unity file on here.
     
    bosstradamus likes this.
  21. dragonvk17

    dragonvk17

    Joined:
    Jun 20, 2019
    Posts:
    2
    I am currently stuck at Chapter 12 about particle. I've done everything according the tutorial, but the smoke particle for the bot won't come out. I've tried to delete it and make a new particle. Until i put it on the bot prefab it shows just alright. But once i put it, the effects won't show on the scene neither the game. On the scene and prefab, there's only a small transparent circle. And on bot's prefab, the smoke's name has a bit transparency, whited out. Sometimes when i try mess around with it, ruby and the bot suddenly run's fast.
    here's an image of it
    here's the code for my bot, hope someone can explain my situation.
    Code (CSharp):
    1. public class EnemyController : MonoBehaviour
    2. {
    3.     public float speed;
    4.     public bool vertical;
    5.     public float changeTime = 3.0f;
    6.     public ParticleSystem smokeParticleEffect;
    7.  
    8.     Rigidbody2D rigidbody2D;
    9.     float timer;
    10.     int direction = 1;
    11.     bool broken = true;
    12.  
    13.     Animator animator;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rigidbody2D = GetComponent<Rigidbody2D>();
    19.         timer = changeTime;
    20.         animator = GetComponent<Animator>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         //remember ! inverse the test, so if broken is true !broken will be false and return won’t be executed.
    27.         if (!broken)
    28.         {
    29.             return;
    30.         }
    31.  
    32.         timer -= Time.deltaTime;
    33.  
    34.         if (timer < 0)
    35.         {
    36.             direction = -direction;
    37.             timer = changeTime;
    38.         }
    39.  
    40.  
    41.         Vector2 position = rigidbody2D.position;
    42.  
    43.         if (vertical)
    44.         {
    45.             animator.SetFloat("Move X", 0);
    46.             animator.SetFloat("Move Y", direction);
    47.             position.y = position.y + Time.deltaTime * speed * direction;
    48.         }
    49.         else
    50.         {
    51.             animator.SetFloat("Move X", direction);
    52.             animator.SetFloat("Move Y", 0);
    53.             position.x = position.x + Time.deltaTime * speed * direction;
    54.         }
    55.  
    56.         rigidbody2D.MovePosition(position);
    57.     }
    58.  
    59.     void OnCollisionEnter2D(Collision2D other)
    60.     {
    61.         RubyController player = other.gameObject.GetComponent<RubyController>();
    62.  
    63.         if (player != null)
    64.         {
    65.             player.ChangeHealth(-1);
    66.         }
    67.     }
    68.  
    69.     //Public because we want to call it from elsewhere like the projectile script
    70.     public void Fix()
    71.     {    
    72.         animator.SetTrigger("Fixed");
    73.         broken = false;
    74.         rigidbody2D.simulated = false;
    75.         smokeParticleEffect.Stop(true);
    76.     }
    77. }
    78.  
     
  22. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    On page 1 of this thread:

     
    dragonvk17 likes this.
  23. dragonvk17

    dragonvk17

    Joined:
    Jun 20, 2019
    Posts:
    2
    thanks, it works now. Although there was something bugging me before. The smokeEffect didn't disappear after i fixed the bot. I've tried
    smokeParticleEffect.Stop();
    and
    smokeParticleEffect.Stop(true);
    yet the smoke still appears after i fix the bot. I didn't know how i fixed it, but i messed around with the animator, try to change the smokeParticleEffect to smokeEffect, tried putting Destroy command instead of stop, but now it just works using the
    smokeEffect.Stop();
    . It's fine now though, so no worries.
    Also sometimes after i make a small change anywhere ruby and the bot runs really fast. After a few changes somewhere else it came back as usual though, so every time that happened i have to make minor changes, like changing the line. I was wondering what might be the cause.
     
    Last edited: Jul 6, 2019
  24. neovonretorch

    neovonretorch

    Joined:
    Jul 5, 2019
    Posts:
    9
    First, I have the same issue as you have for Robot movement. Even though I initiate the value of speed
    Code (CSharp):
    1.     public float speed = 3.0f;
    it actually starts as 0 unless I set it again in Start():
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         // because I don't know why this doesn't work otherwise??!
    4.         speed = 3.0f;
    5.         // ...
    6.      }
    Then, vertical is false, and remains false. I didn't see anywhere in the tutorial code to switch that at any time. So are we just supposed to manually change it in the Inspector for fun? ;)

    Can you provide your "check max health" code? Maybe there's a C# syntax error of some sort?

    Also FYI, while I didn't see this in the tutorial, you can set a second set of timer variables and then switch out the vertical value like so:
    Code (CSharp):
    1.         if (upDownTimer < 0)
    2.         {
    3.             vertical = !vertical;
    4.             upDownTimer = upDownChangeInterval;
    5.         }
     
    Last edited: Jul 8, 2019
    bzjohnson likes this.
  25. CrazedLlamaz

    CrazedLlamaz

    Joined:
    Aug 11, 2018
    Posts:
    4

    I had this issue. The tutorial does not remind you that you need to get the Animator component. Add this under void Start()

    animator = GetComponent<Animator>();
     
  26. CrazedLlamaz

    CrazedLlamaz

    Joined:
    Aug 11, 2018
    Posts:
    4
    I am having the same issues. I can't figure out what is causing the issue. Here is what I have to compare. Also, the prefab is named Projectile.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RubyController : MonoBehaviour
    6. {
    7.     public float speed = 3.0f;
    8.  
    9.     public int maxHealth = 5;
    10.     public float timeInvincible = 2.0f;
    11.  
    12.     public int health { get { return currentHealth;}}
    13.     int currentHealth;
    14.     bool isInvincible;
    15.     float invincibleTimer;
    16.  
    17.     Rigidbody2D rigidbody2D;
    18.     Animator animator;
    19.     Vector2 lookDirection = new Vector2(1,0);
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.        
    25.         animator = GetComponent<Animator>();
    26.  
    27.         rigidbody2D = GetComponent<Rigidbody2D>();
    28.  
    29.         currentHealth = maxHealth;
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         float horizontal = Input.GetAxis("Horizontal");
    36.         float vertical = Input.GetAxis("Vertical");
    37.                
    38.         Vector2 move = new Vector2(horizontal, vertical);
    39.        
    40.         if(!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    41.         {
    42.             lookDirection.Set(move.x, move.y);
    43.             lookDirection.Normalize();
    44.         }
    45.        
    46.         animator.SetFloat("Look X", lookDirection.x);
    47.         animator.SetFloat("Look Y", lookDirection.y);
    48.         animator.SetFloat("Speed", move.magnitude);
    49.        
    50.         Vector2 position = rigidbody2D.position;
    51.        
    52.         position = position + move * speed * Time.deltaTime;
    53.        
    54.         rigidbody2D.MovePosition(position);
    55.  
    56.  
    57.         if(isInvincible)
    58.         {
    59.             invincibleTimer -= Time.deltaTime;
    60.             if(invincibleTimer < 0)
    61.             isInvincible = false;
    62.         }
    63.  
    64.         if(Input.GetKeyDown(KeyCode.C))
    65.         {
    66.             Launch();
    67.         }
    68.  
    69.  
    70.     }
    71.  
    72.     public void ChangeHealth(int amount)
    73.     {
    74.         if(amount < 0)
    75.         {
    76.             if(isInvincible)
    77.             return;
    78.  
    79.             isInvincible = true;
    80.             invincibleTimer = timeInvincible;
    81.         }
    82.  
    83.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    84.         Debug.Log(currentHealth + "/" + maxHealth);
    85.     }
    86.  
    87.     void Launch()
    88.     {
    89.         GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
    90.  
    91.         Projectile projectile = projectileObject.GetComponent<Projectile>();
    92.         projectile.Launch(lookDirection, 300);
    93.  
    94.         animator.SetTrigger("Launch");
    95.     }
    96. }
    97.  
    98. //Projectile class below
    99.  
    100. using System.Collections;
    101. using System.Collections.Generic;
    102. using UnityEngine;
    103.  
    104. public class Projectile : MonoBehaviour
    105. {
    106.  
    107.     Rigidbody2D rigidbody2d;
    108.    
    109.     void Awake()
    110.     {
    111.         rigidbody2d = GetComponent<Rigidbody2D>();
    112.     }
    113.  
    114.     // Update is called once per frame
    115.     public void Launch(Vector2 direction, float force)
    116.     {
    117.         rigidbody2d.AddForce(direction * force);
    118.     }
    119.  
    120.     void OnCollisionEnter2D(Collision2D other)
    121.     {
    122.         //we also add a debug log to know what the projectile touch
    123.         Debug.Log("Projectile Collision with " + other.gameObject);
    124.         Destroy(gameObject);
    125.     }
    126. }
    127.  
     
  27. neovonretorch

    neovonretorch

    Joined:
    Jul 5, 2019
    Posts:
    9
    I don't see the following in your code:
    Code (CSharp):
    1.  
    2.     public GameObject projectilePrefab;
    Once you've added that as a public property on the RubyController, you can go back into Unity and drag the Cog prefab into the slot created in her scripts area in the Inspector.
     
    CrazedLlamaz likes this.
  28. CrazedLlamaz

    CrazedLlamaz

    Joined:
    Aug 11, 2018
    Posts:
    4
    I made the change by adding public GameObject projectilePrefab; to the class before Void Start()

    Now I am getting the following error. No other changes have been made to the previously mentioned code.

    Assets\Scripts\RubyController.cs(91,69): error CS0103: The name 'rigidbody2d' does not exist in the current context
     
  29. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    In your RubyController script, you have named your Rigidbody2D variable rigidbody2D, which is how you have referred to it until Line 89.
     
    Last edited: Jul 10, 2019
  30. bzjohnson

    bzjohnson

    Joined:
    Jul 8, 2019
    Posts:
    1
    Thanks!! I couldn't figure out why the bot wasn't moving at all. This fixed it.
     
  31. CrazedLlamaz

    CrazedLlamaz

    Joined:
    Aug 11, 2018
    Posts:
    4
    This is an edit of my original comment.

    the code provided by the tutorial specified to use rigidbody2D, which is why it is referred to there, after designating that we are using the projectilePrefab (variable was called at the beginning of the script)

    To get rid of the errors and get everything to work, I changed the following under Void Launch()

    Code (CSharp):
    1. GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);
    now reads as

    Code (CSharp):
    1. GameObject projectileObject = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
     
    Last edited: Jul 10, 2019
  32. stephenkim79

    stephenkim79

    Joined:
    May 8, 2019
    Posts:
    6

    Holy F***, thank you. This is my first time really doing any coding or Unity and this was driving me bananas!!!!!
     
  33. stephenkim79

    stephenkim79

    Joined:
    May 8, 2019
    Posts:
    6
    In the Projectiles Tutorial (see below), the tutorial gives the wrong way of opening up the Layer Manager. Clicking on the Layer drop-down from Inspector does nothing. You have to access it as follows: To create a new layer, open the Tags and Layers window (main menu: Edit > Project Settings, then select the Tags and Layers category).



    3. Layers and collisions

    The right way to fix this is to use Layers. Layers allow you to group GameObjects together so they can be filtered. In our case, we will make a Character layer to put our Ruby GameObject in, and a Projectile layer to put all our projectiles in. Then we can tell our physics system that the Character and Projectile layers can’t collide, so it will ignore all collision between objects in those layers.


    To see which layer a GameObject is in, click the Layers drop-down in the top right corner of the Inspector. All objects start in the Default layer (layer number 0). Your game can have up to 32 different layers.






    If you click on it, a popup will show some built-in layer already defined by Unity, but we want to create our own, so choose Add Layer. This will open the Layer manager.
     
    bzjohnson likes this.
  34. stephenkim79

    stephenkim79

    Joined:
    May 8, 2019
    Posts:
    6
    For anyone wondering, this is the code I did to get the projectile to shoot. The instructions didn't specify where some of the portions of code should be inserted, so it was a bit frustrating to figure out what was wrong for a beginner. I had put the Launch function outside the Update function, which was causing an error because lookDirection wasn't defined. I really wish they'd just put the code for each step so you don't get stuck like this. I've also spent hours, trying to figure out what went wrong, only to find that the tutorial was flawed. I suppose it's good practice, but they should give a warning that their tutorial has problems that you might need to do some research online to fix. That way you won't be pulling your hair out wondering why your code isn't working.



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class RubyController : MonoBehaviour
    {
    public float speed = 3.0f;

    public int maxHealth = 5;
    public float timeInvincible = 2.0f;

    public GameObject projectilePrefab; //projectile

    public int health { get { return currentHealth; } }




    private int currentHealth;
    bool isInvincible;
    float invincibleTimer;

    Rigidbody2D rigidbody2d;
    Animator animator;

    // Vector2 lookDirection; //test

    // Start is called before the first frame update
    void Start()
    {
    rigidbody2d = GetComponent<Rigidbody2D>();

    currentHealth = maxHealth;

    animator = gameObject.GetComponent<Animator>();
    }


    // Update is called once per frame
    void Update()
    {
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");

    Vector2 move = new Vector2(horizontal, vertical);

    Vector2 lookDirection = new Vector2(1, 0);

    if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    {
    lookDirection.Set(move.x, move.y);
    lookDirection.Normalize();

    }

    //Debug.Log(lookDirection.x + "/" + lookDirection.y);
    //Debug.Log(move.magnitude);
    animator.SetFloat("Look X", lookDirection.x);
    animator.SetFloat("Look Y", lookDirection.y);
    animator.SetFloat("Speed", move.magnitude);

    Vector2 position = rigidbody2d.position;

    position = position + move * speed * Time.deltaTime;

    rigidbody2d.MovePosition(position);

    if (isInvincible)
    {
    invincibleTimer -= Time.deltaTime;
    if (invincibleTimer < 0)
    isInvincible = false;
    }


    //Projectile
    void Launch()
    {
    GameObject projectileObject = Instantiate(projectilePrefab, rigidbody2d.position + Vector2.up * 0.5f, Quaternion.identity);

    Projectile projectile = projectileObject.GetComponent<Projectile>();
    projectile.Launch(lookDirection, 300);

    animator.SetTrigger("Launch");
    }


    if (Input.GetKeyDown(KeyCode.C))
    {
    Launch();
    }
    }

    public void ChangeHealth(int amount)
    {
    if (amount < 0)
    {
    if (isInvincible)
    return;

    isInvincible = true;
    invincibleTimer = timeInvincible;
    }

    currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);

    Debug.Log(currentHealth + "/" + maxHealth);
    }


    }
     
    bluedjynn30 likes this.
  35. stephenkim79

    stephenkim79

    Joined:
    May 8, 2019
    Posts:
    6
    The creators of this tutorial should really really say where exactly to put the portions of code they add in. It's really frustrating when they're like "oh and we just throw that in...". That being said, it is otherwise a good tutorial, but this kind of stuff really drives beginners away, and I'm under the impression that the tutorial was made for beginners.
     
    bluedjynn30 likes this.
  36. tasnimrushi10

    tasnimrushi10

    Joined:
    Jun 23, 2019
    Posts:
    8
    Hey!!! I've finished the tutorial and build the game successfully. It shows me no problem in unity editor in both scene view and game view. But when I run the .exe file the robot, collectible, damage zone, some tiles from tilemap and the ruby form the head up display disappears. I've attached the image. What is the problem? Can anyone help me?? Also some big trees disapears... ruby.png
     
    Last edited: Jul 12, 2019
  37. stephenkim79

    stephenkim79

    Joined:
    May 8, 2019
    Posts:
    6
    I haven't finished the tutorial fully yet, but when I was making the Health Bar UI, Ruby's head was behind the rest of the UI for some reason (and therefore not showing). It was driving me crazy because the research I did online said that the child of a parent in the UI would normally show above the parent by default. I fixed it by just closing Unity and reopening it, and somehow that fixed it. I hope this info is useful to you.
     
  38. stephenkim79

    stephenkim79

    Joined:
    May 8, 2019
    Posts:
    6
    Is anyone else having trouble with Ruby's animations? The running animations work, but when Ruby is idling, it always shows her facing right. Her hit animation is also not working. Also, when she idles she is always actually facing right, despite having been moving in another direction. I can tell this because when I shoot a cog after stopping, it shoots to the right.
     
    Stew_79 likes this.
  39. tasnimrushi10

    tasnimrushi10

    Joined:
    Jun 23, 2019
    Posts:
    8
    I've reopened unity but still the same problem... :'(
     
  40. tasnimrushi10

    tasnimrushi10

    Joined:
    Jun 23, 2019
    Posts:
    8
    Problem solved. Sorry but I built the game for x86 operating system but mine is x64... happy game developing to all
     
  41. Senethys

    Senethys

    Joined:
    Jul 6, 2019
    Posts:
    1
    "Select the Tileset called FloorBricksToGrassCorner. Just as you did in the previous tutorial, click on the small arrow to see which Sprite was created from the image. At the moment, there is a single Sprite that is the full image. Instead, you need to tell Unity that this image actually contains nine different Sprites."

    How do I know this? Where is it indicated that the tile is made up of 9 different tiles?
     
    Last edited: Jul 14, 2019
  42. AbbyStrawby

    AbbyStrawby

    Joined:
    May 31, 2019
    Posts:
    14
    I'm having the same problem as this person, but the solution he found was a missing brace, i have my braces yet the problem persists. Any ideas for troubleshooting? If somebody already answered this im sorry but i didnt see it anywhere on the forum
     
  43. smithclarkson001

    smithclarkson001

    Joined:
    Jul 15, 2019
    Posts:
    1
    I actually just finished Ruby's adventure a couple days ago and it was pretty good. There were a couple points later on in the tutorial where I got stuck but it was probably my own fault for taking a break in the middle. In the end it worked. 9Apps VidMate app VLC
    I actually started with the 2D game kit before that. It's an even more beginner lesson and I think it helped me get a feel for the editor before delving too deep into
     

    Attached Files:

    Last edited: Feb 20, 2020
  44. Tyler_Darkflame

    Tyler_Darkflame

    Joined:
    Jun 18, 2019
    Posts:
    3
    Hey I just finished the tutorial but I am having trouble running the exe as an app. Here's what is confusing me: I can run the application from the file explorer but when I move the app to the desktop and try to run it from there, I get the error message "UnityPlayer.dll not found". Does anybody know how to fix this?
     
  45. trainoasis

    trainoasis

    Joined:
    Jul 3, 2019
    Posts:
    2
    Have the same issue, smoke not showing, not sure what's causing this yet.
     
    bzjohnson likes this.
  46. Zeketrs

    Zeketrs

    Joined:
    Jul 8, 2019
    Posts:
    1
    So they're actually telling you this individually. If you look at the sprite itself it's made of up nine different sections. The next few steps tell you how to tell unity to extract those nine images from the single sprite. You could actually divide it farther, but that wouldn't make much sense.
     
  47. WanderingKid

    WanderingKid

    Joined:
    Jul 18, 2019
    Posts:
    1
    In Visual styling - Particles, I just had a bit of a few hours of headache trying to figure something out, so hopefully this is useful. So I just went around the world a few times trying to get a custom particle effect to occur on Ruby when she gets hit. Just borrowed the explosion one and have it bursting, nothing fancy. Set it as a prefab named ImpactExplosion and went on my way... I thought.

    I wanted to include it in the RubyController script in the ChangeHealth function. First I ran into all sorts of problems getting things to instantiate correctly. After some twiddling I realized I was was somehow destroying the wrong prefab... which after some searching made me remove the child for the Ruby object.

    Search for the error with this: Unity "The object of type 'particle system' has been destroyed"

    Removing that got rid of the first error I was running into. Just had to hook the prefab object directly to the public variable, included in the class definition at the beginning, pre-function. This will also helped remove hanging objects that would otherwise be cluttering up your hierarchy.

    Code (CSharp):
    1.  
    2. public GameObject projectilePrefab;
    3. public ParticleSystem ImpactExplosion;
    4.  
    Then I had a heck of a time trying to get the particle effect to actually occur. Now, I'm a Unity Newbie, other coding languages not withstanding. For the life of me I couldn't figure out why the smoke off the bot worked just fine but this non-looping burst particle couldn't work. Sure, the tutorial says you have to instantiate it... but that wasn't cutting it.

    More searching, and... voila. You have to actually PLAY the danged thing... so my ChangeHealth (nearing the end of this tutorial lesson) looks like this in RubyController.

    Code (CSharp):
    1.     public void ChangeHealth(int amount)
    2.     {
    3.         if (amount < 0)
    4.         {
    5.             if (isInvicible)
    6.                 return;
    7.  
    8.           ParticleSystem ImpactEvent =  Instantiate(ImpactExplosion, transform.position, Quaternion.identity);
    9.             ImpactEvent.Play();
    10.             animator.SetTrigger("Hit");
    11.             isInvicible = true;
    12.             invincibleTimer = timeInvincible;
    13.          
    14.         }
    15.  
    16.  
    17.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    18.         Debug.Log(currentHealth + "/" + maxHealth);
    19.     }
    Yes, it was that simple... but it took me a few hours to get there. Hopefully this helps someone else.
     
    Pk0n, Nayelle, LLLik and 4 others like this.
  48. edmundito

    edmundito

    Joined:
    Sep 2, 2013
    Posts:
    1
    Hello I'm a bit confused between the tutorial that is in the Unity Learn website and the "Ruby's adventure" project I start from the Unity hub.

    It seems that there is a mismatch in the assets that I get from following the tutorial on the learn site vs what I'm getting in the hub. Which tutorial matches I the project in the hub?
     
  49. MrFunky

    MrFunky

    Joined:
    Jul 28, 2019
    Posts:
    3
    Same here. I've spent 90min tweaking the settings and was close. Then I checked in the complete project how it looks like (You can download the Complete Project and see how it's done).

    For your convenience here is the proper setup:

    Health Collectible Effect:
    PickupEffect_PROPER_setup.png PickupEffect_PROPER_setup_p2.png

    Hit Effect:
    HitParticle_PROPER_setup_p1.png HitParticle_PROPER_setup_p2.png


    EDIT 2:
    While I managed to set up Particle Effect properly for collecting health. I have issues setting up the hit effect. The effect is played out on the spot where Ruby gets hit, so when she's getting hit while running - she get's away while particle is played on the spot where the hit took place. I know that it was intended that way as it's the same in Complete Project, but I like the effect to stay "on her".

    I didn't put the HitEffect particle as a child of Ruby, to avoid playing it on awake. And when I switch the Simulation Space from local to world it follows her, leaving a trail.

    What would be the best way to make the particle stay "on Ruby"?
     
    Last edited: Jul 29, 2019
  50. JuLz_SuN

    JuLz_SuN

    Joined:
    Jul 28, 2019
    Posts:
    4