• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EinfachNurDave · Sep 21, 2020 at 10:02 PM · movementaxiscalculation

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);
 }

} ,

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EinfachNurDave · Sep 17, 2020 at 06:16 PM 0
Share

I would prefer it, to stay with a rigidbody movement

avatar image EinfachNurDave · Sep 18, 2020 at 08:29 AM 0
Share

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);
 }

} `

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with moving my player whilst in the air 1 Answer

Character Axes Rotation Problem 3 Answers

How to control an object with a mouse? 1 Answer

Restricting movement on the vertical axis 1 Answer

Freeze Character control on axis 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges