user10958959
user10958959

Reputation:

Why does my code not run in Unity? Unity C#

For some reason, my code is not working... I have no idea why. can someone help? I am using a switch statement to control my code:

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

public class PlayerMovement : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = transform.position;
        string state = "idle";
        float vx = 0f;
        float vy = 0f;
        float playerSpeed = 2f * Time.deltaTime;

        switch (state) {
            case "idle": 
                vx = 0;
                vy = 0;

                if (Input.GetKey (KeyCode.A)) state = "left";
                if (Input.GetKey (KeyCode.D)) state = "right";
                if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";             
                break;

            case "left":
                vx = -1 * playerSpeed;
                vy = 0;

                if (Input.GetKey (KeyCode.A)) state = "left";
                if (Input.GetKey (KeyCode.D)) state = "right";
                if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";             
                break;

            case "right":
                vx = playerSpeed;
                vy = 0;

                if (Input.GetKey (KeyCode.A)) state = "left";
                if (Input.GetKey (KeyCode.D)) state = "right";
                if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) state = "idle";             
                break;
        }
        vx += pos.x;
        vy += pos.y;
        pos += transform.position;

    }
}

The console has no errors, and I can not see any error with my code...

Please help!

any answers are greatly appreciated.

Thanks.

Upvotes: 0

Views: 209

Answers (2)

Gailbert
Gailbert

Reputation: 115

You see, I feel like the "state" is getting reset to "Idle" everytime. can you try moving that string state = "idle"; Start() function then add this state = "idle"; somewhere else like at the buttom of

   vx += pos.x;
   vy += pos.y;
   pos += transform.position;

You see you have a Switch Case. which will pass the code once, you see those breaks? thats why they don't enter or receive the values change on your vx and vy. I suggest you just use If Else instead of a switch for detecting the input.

Upvotes: 1

Foggzie
Foggzie

Reputation: 9821

You're evaluating your input in each switch case instead of before you evaluate the switch. You're also checking input and then the lack of it so just use else to clean up those checks. You're also doing nothing other than vy = 0 so don't bother setting that:

if (Input.GetKey (KeyCode.A))
    state = "left";
else if (Input.GetKey (KeyCode.D)) // if you hold both A and D, A will get priority
    state = "right";
else
    state = "idle";
switch(state)
{
    case("idle")
        vx = 0;
        break;
    case("left")
        vx = playerSpeed;
        break;
    case("right")
        vx = -1 * playerSpeed;
        break;
}

You're also not adding the values to the position of the transform properly, you're just adding them to your temporary variable, pos (a variable you don't need at all):

vx += pos.x;
vy += pos.y;
pos += transform.position;

should instead be:

transform.position.Translate(vx, vy, 0);

I'd also like to point out that the switch itself is entirely pointless but I made this my answer so it's clear what's being done wrong; you should just be setting your vx values in the if/else if/else statements.

Upvotes: 2

Related Questions