Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Artikon · Nov 04, 2015 at 04:48 PM · c#movement scriptjoystick

4 Way Movement Using Joystick Input

Hi All,

I'm trying to write a C# script for character movement that only allows my character to move in 1 of 4 directions at a time (up, down, left, right). The challenge is that I want this to use joystick input instead of keyboard.

I currently have a script which I found on these forums which works perfectly, except for the fact that it allows 8 way (ie. can move diagonally) movement instead of just 4.

Here is the script I'm currently using. Any tips on how I can update it to restrict diagonal movement would be great.

 public class newMove : MonoBehaviour {
 
     private float speed = 1f;
     public Transform playergraphic;
 
     // Update is called once per frame
     void FixedUpdate()
     {
         Movement();
     }
 
     void Movement()
     {
 
         //Player object movement
         float horMovement = Mathf.Round(Input.GetAxis("Horizontal"));
         float vertMovement = Mathf.Round(Input.GetAxisRaw("Vertical"));
 
         transform.Translate(transform.right * horMovement * Time.deltaTime * speed);
         transform.Translate(transform.forward * vertMovement * Time.deltaTime * speed);
 
         //Player graphic rotation
         Vector3 moveDirection = new Vector3(horMovement, 0, vertMovement);
 
         moveDirection.Normalize();
 
         if (moveDirection != Vector3.zero)
         {
             Quaternion newRotation = Quaternion.LookRotation(moveDirection);
             playergraphic.transform.rotation = Quaternion.Slerp(playergraphic.transform.rotation, newRotation, Time.deltaTime * 5);
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HenryStrattonFW · Nov 06, 2015 at 12:39 PM

The simplest way I can think of to do this would be to discard one of the inputs.

So, you read in your horizontal and vertical axis as you are now, then you check the magnitude of each value, whichever axis has the largest absolute value gets used, and the other gets set to zero, then you apply those values to your movement.

Comment
Add comment · Show 3 · Share
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 Artikon · Nov 07, 2015 at 02:54 PM 0
Share

Hi,

Thanks for the suggestion! I agree with your premise and it's along the same line as what I was thinking.

The challenge I have is that each axis can have an absolute value of 1 or -1, dependant on left/right and up/down. For example, when comparing up and right, I might have the values of 1 and -1. In this case, 1 would win every time, which causes problems.

This is where I'm stuck now. Can't figure out a way to compare 2 numbers regardless of sign (+/-) and get the highest.

avatar image HenryStrattonFW Artikon · Nov 09, 2015 at 10:20 AM 0
Share

Ah, see that's where Absolute comes in. Sorry I should have been clearer, if you do $$anonymous$$athf.Abs(value) then it will return value as exactly the same number if its positive, and if its negative it will return the number as positive as if it didn't have the -ve sign. So $$anonymous$$athf.Abs(-10) returns 10.

This absolute value gives you the magnitude of any number regardless of its sign.

avatar image Artikon HenryStrattonFW · Nov 20, 2015 at 08:20 AM 0
Share

Aha! That's what I was after. Thank you for clarifying your answer. I have other issues with the rotate reverting back when I release the axis trigger now, but that's for another post. Thanks for your help :)

avatar image
7

Answer by YasinJavaid_ · Feb 06, 2018 at 09:14 AM

Late but works perfectly for me.

//Update _InputDir = joystick.GetInputDirection(); // Joy Stick input inputX = _InputDir.x; inputY = _InputDir.y; if (Mathf.Abs (inputX) > Mathf.Abs (inputY)) { inputY = 0; } else { inputX = 0; }

Comment
Add comment · Show 1 · Share
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 Mahian · Sep 02, 2020 at 03:59 PM 0
Share

Thanks, worked perfectly for my 3D top-down scene

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Joystick unusual behavior on axis with almost the same script as W A S D 0 Answers

C# Clamp joystick rotation 0 Answers

Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers


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