Search Unity

How to combine keyboard commands? Movement c#

Discussion in 'Scripting' started by Alex Cruba, Aug 22, 2013.

  1. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    My script moves a cube forward, backward, left and right. I want to enbed a command to turn right left while forward is pressed.

    Any ideas?
     
  2. notpinkiepie

    notpinkiepie

    Joined:
    Jul 2, 2013
    Posts:
    10
    Instead of telling the script specifically "key A means move to the left", you could try something along the lines of this (it's javascript but shouldn't be hard to convert)

    Code (csharp):
    1.     var speed = 5.0;
    2.  
    3.     direction = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    4.     direction = transform.TransformDirection(direction);
    5.     direction *= speed;
    6.    
    7.     controller.Move(direction * Time.deltaTime);
    (make sure controller is set to CharacterController)

    This checks for movement along all axes at the same time, and will allow what you are looking for. This is also the preferred method of simple movement since Unity has a built-in input manager which allows the player to tell the game WHAT they want to be the horizontal/vertical axes.
     
  3. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    I combined it now... problem: I must adjust the y axis of object 90 degrees. Dunno how...
     
  4. notpinkiepie

    notpinkiepie

    Joined:
    Jul 2, 2013
    Posts:
    10
    If the object is aligned improperly, go into the editor and click the cube. Mess with the rotation properties until it works.