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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Moving a character with KeyCombo

Discussion in 'Scripting' started by Bazoozoo, Mar 27, 2015.

  1. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    I've been really stuck trying to script in C# moving a character. I want him to move right using the left and right arrow keys but only one after another. Eg: < > < > < > <> or <><><><><> not >>< >><<><>.

    Any help is appreciated, thanks :D
     
  2. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    The commands are going to depend on whether or not you're using a Character Controller or a rigidbody to move the character.

    If you're using a character controller, just grab the direction of movement with the Input.GetAxis() commands and then transfer the info for that into a movement command.

    //declare variables
    CharacterController controller;
    public float playerSpeed = 10f; //get how fast player moves

    //put this in your Start or Awake function
    controller = GetComponent<CharacterController>(); //fetch Character Controller component data

    //put this in your Update function

    //get user input
    float horizontalMovement = Input.GetAxis("Horizontal");
    float verticalMovement = Input.GetAxis("Vertical");

    //transfer user input into a Vector3
    Vector3 moveDirection = new Vector3(horizontalMovement, 0.0f, verticalMovement);

    //use charactercontroller.move to move player
    controller.Move(moveDirection *Time.deltaTime * playerSpeed);

    Of course, if you're only worried about left and right movements then you can ditch the verticalMovement variables and stick with just your horizontalMovement in your moveDirection like this:

    Vector3 moveDirection = new Vector3(horizontalMovement, 0.0f, 0.0f);


    The thing is that you have to make sure your project Input settings match the string values used in the //get user input section.
     
  3. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    if i understand you correct, you want that your player needs to push the left button before he can move right?

    eg: player wants to go right
    presses: <-; ->

    eg: player wants to go left
    presses: ->; <-

    if thats what you want the only thing you have to do is save pastInput and compare it with currentInput

    Code (CSharp):
    1.  
    2. float currentInput = 0;
    3. float pastInput = 0;
    4. Vector2 moveDirection;
    5. Rigidbody2D myRigidbody;
    6.  
    7. void Awake()
    8. {
    9.     myRigidbody = GetComponent<Rigidbody2D>();
    10. }
    11.  
    12. void Update()
    13. {
    14.   if(pastInput == 0)
    15.   {
    16.       pastInput= ReadInput();
    17.       return;
    18.   }
    19.  
    20.   currentInput = ReadInput();
    21. }
    22.  
    23. void FixedUpdate()
    24. {
    25.   if ( pastInput *-1 == currentInput )
    26.      moveDirection = currentInput;
    27.   else
    28.      moveDirection = 0;
    29.  
    30.     moveDirection.y = myRigidbody.velocity.y;
    31.     myRigidbody.velocity = moveDirection;
    32. }
    33.  
    34.  

    Edit: you have todo:
    + cache Input in Update()
    + move Player in FixedUpdate()

    Comment:
    - no player is that fast.... add a realistic timeSpan between input reading

    Edit 2:

    output of Input.GetAxis("Horizontal") needs to get transformed to -1, 0 and +1 values, nothing else!

    Code (CSharp):
    1. float ReadInput()
    2. {
    3.      float rawInput = Input.GetAxis("Horizontal");
    4.      if( rawInput < 0)
    5.           return -1;
    6.      else if( rawInput > 0)
    7.           return 1;
    8.      else
    9.            return 0;
    10.  
    11. }
     
    Last edited: Mar 27, 2015
  4. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    I probably should've mentioned that this is in 2D. I only want him to move right by pressing LEFT ARROW and then RIGHT ARROW one after another. I'm also using rigidbody.
     
  5. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    then my code suits perfectly. take a look.

    Edit: please change the thread title to a better name
    something like Moving Character with KeyCombo



    Code (CSharp):
    1. float currentInput = 0f;
    2. float pastInput = 0f;
    3. Vector2 moveDirection;
    4. Rigidbody2D myRigidbody;
    5.  
    6. void Awake()
    7. {
    8.     myRigidbody = GetComponent<Rigidbody2D>();
    9. }
    10.  
    11. void Update()
    12. {
    13.   if(pastInput == 0f)
    14.   {
    15.       pastInput= ReadInput();
    16.       return;
    17.   }
    18.   currentInput = ReadInput();
    19. }
    20.  
    21. void FixedUpdate()
    22. {
    23.   if ( pastInput *-1f == currentInput )
    24.      moveDirection.x = currentInput;
    25.   else
    26.      moveDirection = Vector.zero;
    27.   moveDirection.y = myRigidbody.velocity.y;
    28.   myRigidbody.velocity = moveDirection;
    29. }
    30.  
    Code (CSharp):
    1. float ReadInput()
    2. {
    3.      float rawInput = Input.GetAxis("Horizontal");
    4.      if( rawInput < 0f)
    5.           return -1f;
    6.      else if( rawInput > 0f)
    7.           return 1f;
    8.      else
    9.            return 0f;
    10.  
    11. }
     
    Last edited: Mar 27, 2015
  6. Bazoozoo

    Bazoozoo

    Joined:
    Mar 27, 2015
    Posts:
    28
    Compiler error can't convert int or float to Vector2.
     
  7. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    check Edit!

    this error you could find yourself!

    a = 0; // 0 == integer
    a= 0f; //0f == float