Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Movement of the player's character

Discussion in 'Scripting' started by patry242, May 11, 2016.

  1. patry242

    patry242

    Joined:
    Mar 31, 2016
    Posts:
    16
    Hi, I'm trying to create a script in C# to move te player's character, but it just doesn't work.

    This is it:
    script.jpg


    The last one gives me problems, it doesn't work. The horizontal and vertical axes are setted in the Input Manager:

    inputManager.jpg


    What's wrong then??
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    OK, first (rule #1), don't paste a picture of your code... paste your code into code tags.

    Second, I'm not at all clear on what you are trying to do here. First you check for specific keys and move your transform; that's fine. Then you check for a Jump button, and translate up; that's fine too (for starters, anyway). Then, if the mouse is down, you check some input axes... and do absolutely nothing with it.

    You say it "doesn't work," but I don't see how that can be. You haven't told it to do anything, so not doing anything is working, isn't it?

    So, rule #2: any time you feel tempted to type "doesn't work," stop yourself. Instead type a description of (1) what happens, (2) what you expected to happen, and (3) how what happened is different from what you expected.
     
    Hyblademin likes this.
  3. patry242

    patry242

    Joined:
    Mar 31, 2016
    Posts:
    16
    Ok, I'm sorry for the mistakes.

    I want the player moves to the left, right up and down as you move the mouse, but only while right clicked. With that script it doesn't. How can I do it? What am I doing wrong?
     
    Last edited: May 14, 2016
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    No worries, everybody's new at first!

    OK... so I would flip the logic around. Always write your methods to be "lazy," that is, if you can think of an easy and reliable way to bail out early, do it! In this case, it sounds like you don't want to do anything when mouse button 1 is not down. So the first line of Update should be:

    Code (CSharp):
    1. if (!Input.GetMouseButton(1)) return;  // bail out!
    Then you can proceed with the rest of your code, safe in the assumption that if you get that far, you know the appropriate button is down.