Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

X and Y Camera Movement 2 Answers

How can I move a controller in relation to the camera's axis? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Replace Input.GetAxis with Touch 2 Answers

How To Accelerate/Decelerate Gradually With Torque GetAxis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges