Search Unity

2.5D Movement on one axis at a time

Discussion in 'Scripting' started by czokka, Apr 8, 2017.

  1. czokka

    czokka

    Joined:
    Mar 14, 2017
    Posts:
    13
    Hi guys.

    Sorry for all the mistakes this is my first post and pretty new to this whole thing so any constructive criticism is very welcomed.

    I'm a student and currently working on a 2.5D game using a 2d character (assignment requirement) in a 3d environment. However for now im just using a cube as the player. Because i do not have time to animate a full turn around I have to restrict my movement to one axis at a time.

    This is what I currently have. Full and free movement. And the axis lined on the joystick as default.
    Orange representing my range of movement and red the axis.
    current.jpg
    current gif optimised.gif

    But my desired effect would be closer to a controls that is restricted to move on only one axis at a time. Moreover and a sightly different part of my question is also rotating the controller joystick axis by 45 degrees so that it sits intuitively for the players movement as represented on the screen. For example if i push the joystick into the upper right corner my character will begin moving on the x axis in the + direction, and if i pull the joystick to the bottom left quarter the character moves on the x-
    desired.jpg
    desired gif optimised.gif

    I have a very basic grasp of C# as we only started working with it this year in college. The solutions i have tried so far are constraining the rigid body with little success. Im sorry if this sounds like a "do this for me" but im very stuck and desperate. Im looking for anyone who can explain to me how i can tackle this in a logical way or point me in the right direction is all. Cheers in advance.

    Below i included my movement controller

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovementController : MonoBehaviour {
    6.  
    7.     public float speed = 6.0F;
    8.     public float jumpSpeed = 8.0F;
    9.     public float gravity = 20.0F;
    10.     private Vector3 moveDirection = Vector3.zero;
    11.  
    12.     public float curXpos;
    13.     public float curZpos;
    14.  
    15.     public Rigidbody rbdy { get; private set; }
    16.  
    17.     void Awake() {
    18.         rbdy = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     void Update() {
    22.         CharacterController controller = GetComponent<CharacterController>();
    23.         if (controller.isGrounded) {
    24.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    25.             moveDirection = transform.TransformDirection(moveDirection);
    26.             moveDirection *= speed;
    27.             if (Input.GetButton("Jump"))
    28.                 moveDirection.y = jumpSpeed;
    29.  
    30.         }
    31.         moveDirection.y -= gravity * Time.deltaTime;
    32.         controller.Move(moveDirection * Time.deltaTime);
    33.  
    34.         curXpos = transform.position.x;
    35.         curZpos = transform.position.z;
    36.  
    37.         if (Input.GetAxisRaw ("Horizontal") > 0 ) {
    38.             //rbdy.constraints = RigidbodyConstraints.FreezeRotation & RigidbodyConstraints.FreezePositionZ;
    39.             //transform.position.z = curZpos;
    40.         }
    41.         if (Input.GetAxisRaw ("Vertical") > 0 ) {
    42.             //rbdy.constraints = RigidbodyConstraints.FreezeRotation & RigidbodyConstraints.FreezePositionX;
    43.             //transform.position.x = curXpos;
    44.         }
    45.     }
    46. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, since you pointed out you're learning and just want some assistance to point you in a logical direction, I can offer this as a suggestion:
    If one axis' input is greater than another(s), [choose/prefer] that axis for the current frame's movement? That sound like a good start? You might extend that by making each "move" carry on for a certain amount of time, thus enforcing the straight line for a little longer.. hope that helps.
     
  3. czokka

    czokka

    Joined:
    Mar 14, 2017
    Posts:
    13
    Hmm that sounds about right and something similar to what i was thinking. Im finding it tought to convert the logic into code. Ive been brwing the API fro something useful for a few days now but there doesn't seem to be any way to set the input to 0. I can only Input.GetAxisRaw. theres no way fro me to cancel out the other axis if the other one is bigger.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well in my thinking.. I'm thinking that you get the Axis data for one, then for the other. At this point, you have both values, but you haven't moved! :)
    Now, you can compare the values, and move afterwards (on the bigger of the two). So the other axis isn't really cancelled out, it's just that you aren't applying any movement based on it.
    We'll call this stage 1, as it doesn't completely cover "keeping a certain amount of distance done before allowing to change direction" just yet.
     
  5. czokka

    czokka

    Joined:
    Mar 14, 2017
    Posts:
    13
    I actually just solved it. My solution was bit cruder. Thanks to google i found the expressions i was looking for.
    Code (CSharp):
    1. void Update() {
    2.  
    3.         curXmove = Input.GetAxisRaw ("Horizontal");
    4.         curZmove = Input.GetAxisRaw ("Vertical");
    5.  
    6.         if(Mathf.Abs(curXmove) > Mathf.Abs(curZmove)){
    7.             curZmove = 0;
    8.         }else{
    9.             curXmove = 0;
    10.         }
    11.  
    12.         CharacterController controller = GetComponent<CharacterController>();
    13.         if (controller.isGrounded) {
    14.             //moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    15.             moveDirection = new Vector3 (curXmove,0,curZmove);
    16.             moveDirection = transform.TransformDirection(moveDirection);
    17.             moveDirection *= speed;
    18.             if (Input.GetButton("Jump"))
    19.                 moveDirection.y = jumpSpeed;
    20.  
    21.         }
    22.         moveDirection.y -= gravity * Time.deltaTime;
    23.         controller.Move(moveDirection * Time.deltaTime);
    24.     }
    25. }
    it was the Mathf.Abs that i needed. im so happy to have solved this but also mad cause it was so simple. im new to c# and programming in general as i am mainly a 3D artist. But cheers for the help tho.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep, yep, that makes good sense. I wasn't explicit about the absolute value. I'm glad that you have it working.
     
    czokka likes this.