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 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. Something like the zelda links awakening remake.

What im trying to achieve ist that the player rotates to the direction you press and then just goes forwards and this in only 8 directions.

I already got this 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 achieve this ? I hope you understand what i mean. This 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 this angle-snapping script a while 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 things), but this 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.

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

Controller Joystick Hold Delay Logic? 1 Answer

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

Getting Raw Joystick Input via Script 1 Answer

Controlling MouseLook script with keys/joystick 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