Search Unity

Bug movement script only works when the character is selected in the unity editor

Discussion in 'Editor & General Support' started by emmetTheDev, Jul 6, 2021.

  1. emmetTheDev

    emmetTheDev

    Joined:
    Jul 6, 2021
    Posts:
    2
    I created a movement script for my 2d character but it only works when I select the character.
    this is the code I used for movement incase it helps.
    Code (CSharp):
    1.  public float speed = Convert.ToSingle(.25);
    2.     void Update()
    3.     {
    4.  
    5.         if (Input.GetAxis("yaxis") > 0)
    6.         {
    7.             transform.Translate(Vector3.up*speed);
    8.         }
    9.         else if (Input.GetAxis("yaxis") < 0)
    10.         {
    11.             transform.Translate(Vector3.down*speed);
    12.         }
    13.         if (Input.GetAxis("Xaxis") > 0)
    14.         {
    15.             transform.Translate(Vector3.right*speed);
    16.            
    17.         }
    18.         else if (Input.GetAxis("Xaxis") < 0)
    19.         {
    20.             transform.Translate(Vector3.left*speed);
    21.         }
    22.  
    23.         UnityEngine.Debug.Log(speed);
    24.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Are you seeing any errors?

    Did you really name your input axis as "Xaxis" and "yaxis" with different upper/lowercase? That has to match precisely and those are NOT default names, so that's suspect right away.

    If you got this snippet somewhere, might want to just abandon it and try a different tutorial.

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Here's some awesome tutorials to start with:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    Imphenzia: How Did I Learn To Make Games:

     
  3. emmetTheDev

    emmetTheDev

    Joined:
    Jul 6, 2021
    Posts:
    2
    there are no errors when I playtest and the code that i have isn't from one particular tutorial but the code I got wasn't from one particular source but a couple of tutorials and google searches. because of that I think I will try to use some different code because I relies I used vector3 for a 2D game and vector two might fix this issue. and with the advice about tutorials I think I've heard some similar advice about them but I'll keep that in mind next time I use a tutorial.