• 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 pullemfloris · Oct 08, 2021 at 03:08 PM · movementinputcontrollerjoystick

How to get 8 direction Movement with controller sticks | Topdown 3D

Im working on an 3d rpg top down view. Somet$$anonymous$$ng like the zelda links awakening remake.

What im trying to ac$$anonymous$$eve ist that the player rotates to the direction you press and then just goes forwards and t$$anonymous$$s in only 8 directions.

I already got t$$anonymous$$s working with WASD and the dpad, there it obviously works because you cant press in between two buttons if you know what i mean.

But i need a way to clamp the joystick input to only the 8 directions. How can i ac$$anonymous$$eve t$$anonymous$$s ? I hope you understand what i mean. T$$anonymous$$s is the code ive already written. Note that im using the new input system.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     private PlayerInputActions playerInput;
 
     private Rigidbody rb;
 
     [SerializeField]
     private float playerSpeed;
 
     private float angle;
 
     private Quaternion targetRotation;
 
     private Vector2 input;
 
     private Transform cam;
 
     void Awake()
     {
         playerInput = new PlayerInputActions();
         rb = GetComponent<Rigidbody>();
     }
 
     void Start()
     {
         cam = Camera.main.transform;
     }
 
     void Update()
     {
         GetInput();
 
         if (input.x == 0 && input.y == 0) return;
 
         CalculateDirection();
         Rotate();
         Move();
     }
 
     void GetInput()
     {
         input.x = playerInput.Player.Move.ReadValue<Vector2>().x;
         input.y = playerInput.Player.Move.ReadValue<Vector2>().y;
     }
 
     void CalculateDirection()
     {
         if (input.sqrMagnitude > 1.0f)
             input = input.normalized;
 
         angle = Mathf.Atan2(input.x, input.y);
         angle = Mathf.Rad2Deg * angle;
         angle += cam.eulerAngles.y;
     }
 
     void Rotate()
     {
         targetRotation = Quaternion.Euler(0, angle, 0);
         transform.rotation = targetRotation;
     }
 
     void Move()
     {
         //transform.position += transform.forward * 5 * Time.deltaTime;
         rb.velocity = transform.forward * 200 * Time.fixedDeltaTime;
     }
 
     void OnEnable()
     {
         playerInput.Enable();
     }
 
     void OnDisable()
     {
         playerInput.Disable();
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Oct 08, 2021 at 06:09 PM

I made t$$anonymous$$s angle-snapping script a w$$anonymous$$le back. I never found a need for angle offsets, so it's a bit simple in how it works (still calculates a magnitude though, among other t$$anonymous$$ngs), but t$$anonymous$$s should fulfill your needs:

 /// <summary>
 /// Snaps the input Vector2 to the nearest angle, starting from Vector2.right and circling counterclockwise
 /// </summary>
 /// <param name="vector">The vector to be processed</param>
 /// <param name="increments">Number of increments (recommended: power of 2, value of 4 or greater)</param>
 /// <returns></returns>
 public static Vector2 SnapAngle(Vector2 vector, int increments)
 {
     float angle = Mathf.Atan2(vector.y, vector.x);
     float direction = ((angle / Mathf.PI) + 1) * 0.5f; // Convert to [0..1] range from [-pi..pi]
     float snappedDirection = Mathf.Round(direction * increments) / increments; // Snap to increment
     snappedDirection = ((snappedDirection * 2) - 1) * Mathf.PI; // Convert back to [-pi..pi] range
     Vector2 snappedVector = new Vector2(Mathf.Cos(snappedDirection), Mathf.Sin(snappedDirection));
     return vector.magnitude * snappedVector;
 }


Also, another note:
Don't multiply Time.fixedDeltaTime into your velocity!
 rb.velocity = transform.forward * 200 * Time.fixedDeltaTime;

... is the same as...

 rb.velocity = transform.forward * 200f / 50f;
 rb.velocity = transform.forward * 4f;
 rb.velocity = transform.forward * playerSpeed; // (playerSpeed = 4f)
Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

222 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

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

How do I listen for Input from joystick/buttons on a controller? 1 Answer

*has been resolved and deleted* 1 Answer

My Players speed differs depending on the input method (gamepad or keyboard) 1 Answer

My character has stopped moving 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