- Home /
How to make 2D Movement with only 1 Axis at the same Time,How to make 2d Movement on 1 Axes at the same time?
I want that my Player can only walk on 1 Axis at the time. It should depend on wich button was pressed first if both axis are called
It doesnt seem to work like i want to heres my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float movementSpeed;
public Rigidbody2D rb;
private float moveX;
private float moveY;
private Vector2 moveDirection;
void Awake() {
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
}
void Update()
{
InputCalculation();
}
void InputCalculation() {
moveX = Input.GetAxisRaw("Horizontal");
moveY = Input.GetAxisRaw("Vertical");
moveDirection = new Vector2(moveX,moveY);
}
private void FixedUpdate() {
if ( moveX != 0.0f || moveY != 0.0f ) // if player asked to move
{
// if the pressure is more important on the horizontal axis
if ( Mathf.Abs(moveX) >= Mathf.Abs(moveY) ) // if both values are equals the default behaviour will be to chose horizontal axis.
{
MoveX();
}
else
{
MoveY();
}
}
}
void MoveX() {
rb.velocity = new Vector2(moveDirection.x * movementSpeed, 0);
}
void MoveY() {
rb.velocity = new Vector2(0, moveDirection.y * movementSpeed);
}
} ,
I would prefer it, to stay with a rigidbody movement
OK: Ive found a solution, wich is probably not the best but its ok. Now, i check if i am currently moving in an axis each frame, Problem: The if-statement i am checking first, is priority, means: if i am moving on the x achsis and then i want to move on the y, it will change my direction of movement. But if i am moving on the y, and then want to move on the x, my movement on the y will stay until i led my fingers from the y.
If anyone has a better Solution, pls help me`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public float movementSpeed;
public Rigidbody2D rb;
private float moveX;
private float moveY;
private bool can$$anonymous$$oveOnY = true;
private bool can$$anonymous$$oveOnX = true;
private Vector2 moveDirection;
void Update()
{
InputCalculation();
}
void InputCalculation() {
moveX = Input.GetAxisRaw("Horizontal");
moveY = Input.GetAxisRaw("Vertical");
moveDirection = new Vector2(moveX,moveY);
}
private void FixedUpdate() {
if (moveDirection.y >= 1 && can$$anonymous$$oveOnY || moveDirection.y <= -1 && can$$anonymous$$oveOnY) {
can$$anonymous$$oveOnX = false;
$$anonymous$$oveY();
}
if (moveDirection.x >= 1 && can$$anonymous$$oveOnX || moveDirection.x <= -1 && can$$anonymous$$oveOnX) { can$$anonymous$$oveOnY = false; $$anonymous$$oveX();
}
}
void $$anonymous$$oveX() {
rb.velocity = new Vector2(moveDirection.x * movementSpeed, 0);
}
void $$anonymous$$oveY() {
rb.velocity = new Vector2(0, moveDirection.y * movementSpeed);
}
private void LateUpdate() {
if (!can$$anonymous$$oveOnX)
{
can$$anonymous$$oveOnX = true;
}
if (!can$$anonymous$$oveOnY)
{
can$$anonymous$$oveOnY = true;
}
rb.velocity = new Vector2(0,0);
}
} `