Search Unity

Moving a character in a top-down 2D game

Discussion in 'Scripting' started by Endarire, Oct 22, 2018.

  1. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    EDIT: This works now! Alleluia! kdgalla mentioned that one class was misnamed, meaning inheritance from this class was impossible.

    Greetings, all!

    After following this video tutorial on making a Zelda 3-like game in Unity 2018.2.13f1 Personal 64-bit, when clicking Play I was told I had errors in my code that demanded they be fixed before my game would start.

    To my knowledge, all my scripts are correct: I copied them from the video; however, there may have been some Unity editor changes since this was filmed that caused the errors. Using Visual Studio Code (VSCode) instead of the default editor or Visual Studio 201X may have also caused problems.

    The intent of the following scripts is to make a character move in 8 directions using the arrow keys but face in 4, akin to Zelda 3.

    Code (CSharp):
    1. //CharacterMovementBaseControl.cs: No errors.
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Input : MonoBehaviour
    8. {
    9.     protected CharacterMovementModel MovementModel;
    10.  
    11.     void Awake()
    12.     {
    13.         MovementModel = GetComponent<CharacterMovementModel>();
    14.     }
    15.  
    16.     protected void SetDirection(Vector2 direction)
    17.     {
    18.         MovementModel.SetDirection(direction); //VS Code says there's a problem with SetDirection().
    19.     }
    20. }

    Code (CSharp):
    1. //CharacterMovementModel.cs: No errors.
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class CharacterMovementModel : MonoBehaviour
    8. {
    9.     //Public Variables
    10.     public float Speed;
    11.  
    12.     //Private Variables
    13.     private Vector3 MovementDirection;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         ;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update ()
    23.     {
    24.         UpdateMovement();
    25.     }
    26.  
    27.     public void SetDirection(Vector2 direction)
    28.     {
    29.         MovementDirection = new Vector3(direction.x, direction.y, 0);
    30.     }
    31.  
    32.     void UpdateMovement()
    33.     {
    34.         if(MovementDirection == Vector3.zero)
    35.         {
    36.             return;
    37.         }
    38.  
    39.         MovementDirection.Normalize();
    40.  
    41.         transform.position += MovementDirection * Speed * Time.deltaTime; //Frame independent movement
    42.     }
    43. }

    Code (CSharp):
    1. //CharacterMovementKeyboardControl
    2.  
    3. //6 ERRORS listed below in comments!
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7.  
    8. //ERROR for CharacterMovementBaseControl: "The type or namespace 'CharacterMovementBaseControl' could not be found. (Are you missing a using directive or an assembly reference?)"
    9. public class CharacterMovementKeyboardControl : CharacterMovementBaseControl
    10. {
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.    
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.    
    22.     }
    23.  
    24.     void UpdateDirection()
    25.     {
    26.         Vector2 newDirection = Vector2.zero; //Set direction to neutral upon movement stop.
    27.  
    28.         //ERROR: These GetKey functions have the error, "'Input' does not contain the definition for 'GetKey' [Assembly-CSharp]"
    29.         if(Input.GetKey(KeyCode.UpArrow) )
    30.         {
    31.             newDirection.y = 1;
    32.         }
    33.  
    34.         if(Input.GetKey(KeyCode.DownArrow) )
    35.         {
    36.             newDirection.y = -1;
    37.         }
    38.  
    39.         if(Input.GetKey(KeyCode.LeftArrow) )
    40.         {
    41.             newDirection.x = -1;
    42.         }
    43.  
    44.         if(Input.GetKey(KeyCode.RightArrow) )
    45.         {
    46.             newDirection.x = 1;
    47.         }
    48.  
    49.         //ERROR: "The name 'SetDirection' does not exist in the current context. [Assembly-CSharp]"
    50.         SetDirection(newDirection);
    51.     }
    52. }
    53.  
     
    Last edited: Oct 23, 2018
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    Are there any error messages in Console window?
     
  3. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    The one error that appeared was this in regards to CharacterMovementBaseControl: "The type or namespace 'CharacterMovementBaseControl' could not be found. (Are you missing a using directive or an assembly reference?)"
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Did you write the code for CharacterMovementBaseControl? If it's a third party script, you may need to add a "using" directive if it's in a namespace.
     
  5. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    I wrote the code for CharacterMovementBaseControl. Even with a 'using CharacterMovementBaseControl' at the top, nothing seemed to change.

    Furthermore, Input.<whatever> seemingly stopped allowing GetKey and GetAxis. Visual Studio Community 2017 and Visual Studio Code had the same problems, leading me to believe this is a Unity problem. (Even copy-pasting sample code from official documentation didn't work, but displayed the same error.)
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    That wouldn't work anyway. "Using" is for namespaces, not class names.

    Are you sure that CharacterMovementBaseControl doesn't have any compiler errors? If so, it would explain why that "type" could not be found (because the definition couldn't compile), but if that were the case, you should see these errors in the console as well.
     
  7. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    I have rechecked CharacterMovementBaseControl and, indeed, it has 0 listed errors.

    If I comment out all of CharacterMovementKeyboardControl and choose Play from the editor, the program runs, but, predictably, didn't allow keyboard commands to move the sprite.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    In that case, I don't know why you get the "The type or namespace 'CharacterMovementBaseControl' could not be found" error. CharacterMovementBaseControl is a class, defined in a *.cs file in your project, and it definitely has no errors, right? Have you tried simply restarting the Unity editor? Could be one of those buggy compiler glitches. :confused:

    If that doesn't work, maybe you could you post the code for that base class, maybe?
     
  9. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    The code for these 3 files is already posted above. The first line of each code segment lists what file it is.
     
  10. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    I don't see a definition for a class called CharacterMovementBaseControl in that file. Also, I wouldn't use the name Input for a class definition because Unity uses that name already.

    Edit: that's most-likely your problem. In order for CharacterMovementBaseControl to exist, there needs to be a class definition for it. Just having a file with that name doesn't count for anything as the compiler doesn't take file names in to account

    Actually, I'm surprised it didn't give you an error message about that file. Usually for monobehaviours, Unity insists that the class name matches the file name (precompilation). Maybe they removed that constraint now that Unityscript is deprecated?
     
    Last edited: Oct 23, 2018
  11. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    I posted the code files in the first post, each labeled on the top line with its file name. (The third file, CharacterMovementKeyboardControl, lacked this filename at the top since this board thought that was spam.)

    Regardless of whether this script is attached to a sprite, the problem persisted. This seems more like a Unity bug.
     
  12. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    What I'm saying is: the error message is saying that it can't find a definition for the CharacterMovementBaseControl class. It needs a definition for CharacterMovementBaseControl class because your CharacterMovementKeyboardControl class supposedly inherits from CharacterMovementBaseControl.

    If that's your code at the top of the post, then it looks like you didn't write a class definition for CharacterMovementBaseControl. You may have a file called CharacterMovementBaseControl.cs, but that file does not contain a definition for a class called CharacterMovementBaseControl, so you still don't have a class called CharacterMovementBaseControl.

    Basically you need to write the actual code for a class called CharacterMovementBaseControl, unless it's there and I'm just not seeing it.
     
    Last edited: Oct 23, 2018
  13. Endarire

    Endarire

    Joined:
    Jul 3, 2012
    Posts:
    14
    kdgalla: Thankee for mentioning that the class was wrongly named. After fixing that, all the errors ceased! Alleluia!
     
  14. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639