Search Unity

Ruby's 2D Adventure: Enemy Script not Working

Discussion in '2D' started by girlinorbit, May 9, 2019.

  1. girlinorbit

    girlinorbit

    Joined:
    Apr 27, 2019
    Posts:
    115
    I am using the 'Ruby's 2D Adventure' tutorial to get a basic introduction into Unity, and I've hit a pretty big stumbling block. I am as far as 'World Interactions - Damage Zones & Enemies' part 4, and am stuck on the script. The script 'solution' given does not work for me. It is attached to the prefab, and I get not errors.
    .
    The enemy is a robot who is supposed to move up and down. Here is the script. I am using Unity version 2018.3, which is the suggested version for this course.

    EnemyControler.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyController : MonoBehaviour
    6. {
    7.     public float speed = 3.0f;
    8.     public bool vertical;
    9.     public float changeTime = 3.0f;
    10.  
    11.     Rigidbody2D rigidbody2D;
    12.     float timer;
    13.     int direction = 1;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rigidbody2D = GetComponent<Rigidbody2D>();
    19.         timer = changeTime;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         timer -= Time.deltaTime;
    26.  
    27.         if (timer < 0)
    28.         {
    29.             direction = -direction;
    30.             timer = changeTime;
    31.         }
    32.  
    33.  
    34.         Vector2 position = rigidbody2D.position;
    35.  
    36.         if (vertical)
    37.         {
    38.             position.y = position.y + Time.deltaTime * speed * direction;
    39.         }
    40.         else
    41.         {
    42.             position.x = position.x + Time.deltaTime * speed * direction;
    43.         }
    44.  
    45.         rigidbody2D.MovePosition(position);
    46.     }
    47. }
    Thanks in advance
     
    BryanLyon likes this.
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Looks fine to me as a standalone script. I would guess the instance in your game isn't the same as your prefab. Or you have the script disabled. Or one of a million things in unity that can go wrong. The script itself looks fine tho.
     
  3. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    Your trying to move a rigidbody in update, you should be using FixedUpdate for any physics

    Also make sure it actually finds the rigidbody component and you are using the right component, Rigidbody2D and not Rigidbody on your object
     
  4. BryanLyon

    BryanLyon

    Joined:
    Jun 3, 2019
    Posts:
    1
    What does that mean "Your trying to move a rigidbody in update", and "You should be using FixedUpdate for any physics"?

    I am doing this tutorial and am having this same problem. I fix or alternative would be very helpful or an explanation about "Update", and "FixedUpate".
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
     
  6. Kenbev84

    Kenbev84

    Joined:
    Mar 3, 2019
    Posts:
    2
    Is it that your enemy is just moving horizontally? The script won't automatically change the enemy from going side-to-side to going up-and-down. As bool vertical is public it creates a check box in the Unity editor. Ticking that should make the enemy move vertically
    upload_2019-6-11_0-48-10.png

    Apologies if I misunderstood your problem.
     
  7. dwest13542

    dwest13542

    Joined:
    Jun 2, 2019
    Posts:
    1
    My enemy isn't moving at all and gives the error that it hides its inherited member
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I thought in your first post you said that
    there were no errors. What line does the error occur on?
     
  9. Kenbev84

    Kenbev84

    Joined:
    Mar 3, 2019
    Posts:
    2
    Is this the error you are getting?
    upload_2019-6-11_20-1-59.png

    When I click on it it takes me to this line.
    upload_2019-6-11_20-3-31.png

    If you put "new" in front of it it will get rid of the error... Well it did for me anyway.
    upload_2019-6-11_20-4-32.png

    As to why the enemy is not moving, can't really tell without seeing your script.
     
  10. jarsman

    jarsman

    Joined:
    Jan 12, 2019
    Posts:
    1
    if enemy not moving, maybe you add script on gameobject directly not prefabs so script not updating. try delete enemy gameobject, then add again from prefabs folder
     
  11. unity_xMf1nULaDYDDRw

    unity_xMf1nULaDYDDRw

    Joined:
    Jul 25, 2019
    Posts:
    2
    My Robot enemy is moving but Ruby main character not get damage when she touched the robot or collide with him.plz help.

    This is my enemy script


    public class EnemyController : MonoBehaviour
    {
    public float speed;
    public bool vertical;
    public float changeTime = 3.0f;

    Rigidbody2D rigidbody2D;
    float timer;
    int direction = 1;


    // Start is called before the first frame update
    void Start()
    {
    rigidbody2D = GetComponent<Rigidbody2D>();
    timer = changeTime;
    }

    // Update is called once per frame
    void Update()
    {

    timer -= Time.deltaTime;

    if (timer < 0)
    {
    direction = -direction;
    timer = changeTime;
    }

    Vector2 position = rigidbody2D.position;
    if (vertical)
    {
    position.y = position.y + Time.deltaTime * speed;
    }
    else
    {
    position.x = position.x + Time.deltaTime * speed;
    }

    rigidbody2D.MovePosition(position);
    }

    void OnCollisionEnter2D(Collision2D other)
    {
    RubyController player = other.gameObject.GetComponent<RubyController>();

    if (player != null)
    {
    player.ChangeHealth(-1);
    }
    }

    }
     
  12. unity_oJwimnOtq-D9ig

    unity_oJwimnOtq-D9ig

    Joined:
    Jul 29, 2019
    Posts:
    2
    same issue here. robot enemy is not moving. the script looks the same as above.
     
  13. unity_oJwimnOtq-D9ig

    unity_oJwimnOtq-D9ig

    Joined:
    Jul 29, 2019
    Posts:
    2
    solved it, hadn't set my speed so it was at 0.
     
  14. ed_s

    ed_s

    Unity Technologies

    Joined:
    Apr 17, 2015
    Posts:
    165
  15. Lickbeans

    Lickbeans

    Joined:
    Jan 18, 2017
    Posts:
    1
    Yeah, despite the script saying "3.0f" the Inspector window still shows "0" so you have to set a number there. I'm using 2019.2.0.

    One problem I noticed is that the robot guy will move when Ruby touches him. She can basically move him around the map. Granted, I'm still only on that particular chapter. Haven't gone further yet.
     
  16. FloryanAiros

    FloryanAiros

    Joined:
    Mar 25, 2020
    Posts:
    2
    maybe a bit late but you need to make sure that in the inspector window in the enemy controller script the value of speed isnt set on 0, but 1 or 2
     
    Skaros143 likes this.
  17. AvoG

    AvoG

    Joined:
    May 12, 2020
    Posts:
    1
    mine doesnt turn arround, starts going right, or up if i check the "vertical" in the inspector, but only goes in one direction. anyone know how to fix this?
     
  18. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Similarly, check that the Change Time variable in the Enemy Controller Script in the Inspector Window is not set to 0.
     
  19. QUIDSQUID

    QUIDSQUID

    Joined:
    Jul 13, 2020
    Posts:
    1
    So I had a couple issues and here is how I fixed them:

    1) My bot didn't have Rigid Body 2D with gravity set to 0. Somehow this got lost when I was playing around with my prefabs. It didn't throw an error in the code, so it wasn't until I checked what modules I had on my prefab I realized anything was wrong.

    2) In the example code, they have public class EnemyController2 : MonoBehaviour. This needs to match the script file name, so I updated this to public class EnemyController : MonoBehaviour

    3) Set the speed of the bot either in the code to start at not zero, or update it when you start the game in the inspector window