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

How to move along the z axis from Top-Down Perspective?

Discussion in 'Scripting' started by RuanJacobz, Apr 13, 2015.

  1. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Hey guys, I've decided to make my 2D game play along the x and z axis, so i can make use of things like the navmesh. But i dont know how to make my character move forward and backward. The movement script looked like this when i used the default 2D view.

    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         transform.position += new Vector3(Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0.0f) * Time.deltaTime * speed;
    4.     }
    5. }
    6.  
    But this obviously doesn't work because my character tries to move up in the air now from the new angle. Can someone help me change this?
     
    Last edited: Apr 13, 2015
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Well, your vector says new Vector3(x,y,z) , so Horizontal goes to x, and vertical to y. z stays at zero. So you should assign your horizontal or vertical thing to the z axis instead of y.
     
    NinjaGames1 likes this.
  3. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    I wanted to mate, but the unity input manager doesn't seem to have the option. It has x and y, and then 3 to 20th joystick axis (and I don't even really know what those mean).
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    The controller input for the joystick is horizontal x and vertical y. You need to assign the axis within your code, not search for a z axis in the joysticks, as this would be a fancy controller ;)

    You have got your right left joystick assigned to x and top down joystick assigned to y.
    Code (CSharp):
    1.         transform.position += new Vector3(Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0.0f) * Time.deltaTime * speed;
    2.  
    but you need to assign your up and down joystick to z, so:
    Code (CSharp):
    1.         transform.position += new Vector3(Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical")) * Time.deltaTime * speed;
    2.  
    As I do not know your specific environment, this is just a suggestion, hope it helps you.
     
    NinjaGames1 and RuanJacobz like this.
  5. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Dude! I can't believe I didn't realize this. Thank you so much. It was ages ago that I wrote that down as I followed a tutorial and I completely forget those values are for each individual axis. Z was set to 0.0f (facepalm).
     
    WheresMommy likes this.
  6. Philipp_Programmer_Germany

    Philipp_Programmer_Germany

    Joined:
    Apr 8, 2015
    Posts:
    9
    And a Tip Jaxobz use Character Controller instead ;)
     
    WheresMommy likes this.