Search Unity

Character Controller: Swapping axes

Discussion in 'Scripting' started by McHazard, Sep 21, 2014.

  1. McHazard

    McHazard

    Joined:
    May 8, 2013
    Posts:
    13
    So, I'm still rather inexperienced with this but I'm trying. I code for a living so I'm totally open to the firehose info dump, but I'm afraid I can't find the magical word incantation to make the Google find what I'm looking for.

    Please forgive my uninformed terminology, but here's the problem:

    I'm writing a top-down shooter. I am using a nice camera that I downloaded from the Asset Store, https://www.assetstore.unity3d.com/en/#!/content/11508. I learned a lot from reading and tweaking it, but I'm clearly misunderstanding something fundamental about Unity because I can't make it work quite the way I want. Basically, 'w' is forward and 's' is backward... I think that is inherited from the C# class CharacterController. Unmodified, the script changes 'w' (or, as I think the code puts it, player.transform.forward) to point toward the mouse. I have commented out a line that rotates the character controller to point to the mouse, but left the part that rotates the model. Now I want to use the same basic logic to just swap the x and z axes; that is, I want to make 'w' move my character 'up' along the x axis rather than 'up' along the z axis.

    However, my attempts to change the code seem to result in either no change, or in unwanted changes. Snippet of what I thought the modification should look like (for obvious reasons I can't post any of the code from the script that I bought):

    controller.transform.rotation = Quaternion.Euler(new Vector3(0, 90, 0));

    I'm pretty sure there's an obvious answer to this beyond rebinding keys. I just don't know what it is =p
     
    Last edited: Sep 21, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You could just simply change the input axes under edit->project settings->input
    Swap the keys for the the Horizontal and Vertical axes.

    http://docs.unity3d.com/Manual/class-InputManager.html

    Other than that, it depends entirely on how your characterController script is set up.
    If it uses input to calculate something like a movementOffsetVector, which is then sent to controller.Move(xxx), you could rotate that by 90 immediately before
    movementOffsetVector = Quaternion.Euler(0,90,0) * movementOffsetVector;