Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I set up 3D character movement?

Discussion in 'Input System' started by MirrorInstinct, Dec 9, 2019.

  1. MirrorInstinct

    MirrorInstinct

    Joined:
    Sep 2, 2019
    Posts:
    41
    (Fairly New Unity User here)

    So I am thrilled to be using the new input system and I am trying to make a 3rd person game but I am having a hard time making the player move in the usual 8 directions that 3rd person games let you move in such as Super Mario Odyssey. I have been using Brackeys' new Unity input system video as a guide for making 3D movement. Essentially, the code I have is the same as his but I changed certain names around. Ive tried changing the Vector2 values to Vector3 but Unity gives me an error saying the game controller stick works only as a Vector2 and that's a problem because then my character doesn't move how they should. I understand that using the same code that he did will only make the character move left, right and directly up but I am not sure how set up code to move my character forward, backwards and side-to-side. Is it something that is doable with Vector2 and I am just not getting it or do I need to somehow use a Vector3?

    This is what I have been using so far:



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;

    public class PlayerControls : MonoBehaviour
    {
    PlayerInput input;

    Vector2 move;

    void Awake()
    {
    input = new PlayerInput();

    input.Gameplay.MovePlayer.performed += ctx => move = ctx.ReadValue<Vector2>();

    input.Gameplay.MovePlayer.canceled += ctx => move = Vector2.zero;
    }

    void Update()
    {
    Vector2 m = new Vector2(-move.x, move.y) * Time.deltaTime;
    transform.Translate(m, Space.World);
    }

    void OnEnable()
    {
    input.Gameplay.Enable();
    }

    void OnDisable()
    {
    input.Gameplay.Disable();
    }
    }


    BONUS: I eventually would like to set up a 3D camera as well so if anyone can give me tips on that, you are awesome.
     
  2. Deozaan

    Deozaan

    Joined:
    Oct 27, 2010
    Posts:
    707
    You need to familiarize yourself with Unity's axis orientation.

    The x axis is left and right.
    The y axis is up and down.
    The z axis is forward and back.

    So you want the up and down motion on the joystick to apply to the z axis instead of the y axis.

    Or in other words, you need to convert the Vector2 input to Vector3 coordinates and move the character on the x and z axes instead of the x and y axes.

    Your update function should contain something like this:
    Code (CSharp):
    1. Vector3 m = new Vector3(-move.x, 0, move.y) * Time.deltaTime;
    Notice I put 0 for the y value and used move.y as the z value.
     
    Last edited: Dec 10, 2019
  3. Deozaan

    Deozaan

    Joined:
    Oct 27, 2010
    Posts:
    707
    Setting up a 3D camera will be similar. You'll want to apply the Vector2 input of the right joystick to different axes as appropriate.

    Because handling rotations (quaternions) can be tricky, I'd suggest it is easiest to make the camera a child of an "empty" GameObject that is centered on the focal point you want to rotate around. And make that a child of yet another empty GameObject at the same location. Use left and right to rotate one of the objects side to side, and up and down to rotate the other one up and down.

    You can then offset your camera back away from the parent objects and parent the whole contraption to your character and it will automatically follow your character around wherever you go.

    Here's an attempt at an illustration of the hierarchy I'm trying to describe:

    Character Object
    --> Left/Right anchor
    ------> Up/Down anchor
    ------------> Camera

    Also keep in mind that rotation changes the axis you want to move along. So if you want to rotate left and right you want to rotate along the y axis. Rotating up and down would be along the x axis. So the x input from the joystick should be used to rotate on y and the y input should be used to rotate on x.

    Furthermore, if it feels like it goes the opposite way than what you're pressing, you may need to multiply that input by -1 to invert the value. You'll have to test it and see what feels right to you. Most games include an option to invert the axis because different people prefer it going different ways.
     
  4. MirrorInstinct

    MirrorInstinct

    Joined:
    Sep 2, 2019
    Posts:
    41
    Thank you so much for your help, I got it up and running. So simple yet it's crazy how it also isn't. Thank you as well for the tip on setting up a follow camera, I really appreciate it!