Search Unity

Diagonal movement help

Discussion in '2D' started by mcdude153, Jun 5, 2019.

  1. mcdude153

    mcdude153

    Joined:
    Jul 20, 2018
    Posts:
    1
    I can't seem to run this in unity, it keeps showing error CS0103, can someone pls help? (I'm just starting so pls don't be mean)
    here is my code:


    void Update()

    {

    //Horizontal and vertical variables

    float HorValue = Input.GetAxis("Horizontal");

    float VerValue = Input.GetAxis("Vertical");

    print (HorValue);

    print (VerValue);



    if (HorValue > 0 & VerValue > 0) {float RightUp = 1;};

    if (HorValue > 0 & VerValue < 0) {float RightDown = 1;};

    if (HorValue < 0 & VerValue > 0) {float LeftUp = 1;};

    if (HorValue < 0 & VerValue < 0) {float LeftDown = 1;};



    //Key press detection

    float VerPosition = transform.position.y;

    float HorPosition = transform.position.x;

    if (HorValue > 0) {transform.position = new Vector3(HorPosition+0.01f, VerPosition, 0f);};

    if (HorValue < 0) {transform.position = new Vector3(HorPosition-0.01f, VerPosition, 0f);};

    if (VerValue > 0) {transform.position = new Vector3(HorPosition, VerPosition+0.01f, 0f);};

    if (VerValue < 0) {transform.position = new Vector3(HorPosition, VerPosition-0.01f, 0f);};



    if (RightUp = 1) {transform.position = new Vector3(HorPosition+0.01f, VerPosition+0.01f, 0f);};

    if (RightDown = 1) {transform.position = new Vector3(HorPosition+0.01f, VerPosition-0.01f, 0f);};

    if (LeftUp = 1) {transform.position = new Vector3(HorPosition-0.01f, VerPosition+0.01f, 0f);};

    if (LeftDown = 1) {transform.position = new Vector3(HorPosition-0.01f, VerPosition-0.01f, 0f);};



    }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    216
    upload_2019-6-5_8-5-2.png

    I just googled that real quick. Where are you declaring RightUp, RightDown, LeftUp and LeftDown? Also, I just noticed that you have unnecessary semi-colons after all of your if statements. You placed them correctly after the transform.position lines, but then you place one after each curly bracket }; which you cannot do.
     
  3. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Just to extend on the above you needed to declare your variables outside of the statements. By declaring them within the brackets of the if statement they only exist with the brackets and can not be used outside of them. Which makes the rest of your statements throw an error. Plus on a side not you cannot use a single = in a if statement it must be if(LeftUp == 1). By using only a single = you're saying LeftUp is equal to 1 but using == you're asking IF it is equals 1
     
unityunity