Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 stefanvlad199403 · Feb 26, 2021 at 12:25 PM · uimovement scriptaxisjoystickjoysticks

How to remove the Vertical axis on Joystick?

Hello! I'm new on this so I don't realy know how to remove the Vertical movement of my joystick, so I will realy apreciate if someone can help me! here is the code and a picture with my Joystick.In this moment my Joystick can be moved in circle, even if the image shows that is only for right and left movement, and i dont know how to "block"or "stop" the movement in circle and up and down.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;

public class JoyStick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { public RectTransform pad; public Transform player; Vector3 moveForward; Vector3 moveRotate; public float moveSpeed; public float rotateSpeed;

 public void OnPointerDown(PointerEventData eventData)
 {
     StartCoroutine("PlayerMove");
 }

 public void OnPointerUp(PointerEventData eventData)
 {
     transform.localPosition = Vector3.zero;
     moveForward = Vector3.zero;
     moveRotate = Vector3.zero;
     StopCoroutine("PlayerMove");
 }

 public void OnDrag(PointerEventData eventData)
 {
     transform.position = eventData.position;
     transform.localPosition = Vector2.ClampMagnitude(eventData.position - (Vector2)pad.position, pad.rect.width * 0.3f);

     //moveForward = new Vector3(0, 0, transform.localPosition.y).normalized;
     moveRotate = new Vector3(0, transform.localPosition.x, 0).normalized;
 }

 IEnumerator PlayerMove()
 {
     while (true)
     {
         player.Translate(moveForward * moveSpeed * Time.deltaTime);

         if (Mathf.Abs(transform.localPosition.x) > pad.rect.width * 0.2f)
             player.Rotate(moveRotate * rotateSpeed * Time.deltaTime);

         yield return null;
     }
 }

}

untitled1.png (181.7 kB)
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
Best Answer

Answer by pauldarius98 · Feb 26, 2021 at 12:35 PM

The problem is at line 17 where you calculate the local position for x and y (y should be constant and 0 most likely) so the code should look like this:

      public void OnPointerDown(PointerEventData eventData)
      {
          StartCoroutine("PlayerMove");
      }
      public void OnPointerUp(PointerEventData eventData)
      {
          transform.localPosition = Vector3.zero;
          moveForward = Vector3.zero;
          moveRotate = Vector3.zero;
          StopCoroutine("PlayerMove");
      }
      public void OnDrag(PointerEventData eventData)
      {
          transform.position = eventData.position;
          var maxXValue = pad.rect.width * 0.3f; 
          var xPosition = Mathf.Clamp(eventData.position.x - pad.position.x, -maxXValue, maxXValue)
          transform.localPosition = new Vector2(xPosition, 0f);
          //moveForward = new Vector3(0, 0, transform.localPosition.y).normalized;
          moveRotate = new Vector3(0, transform.localPosition.x, 0).normalized;
      }
      IEnumerator PlayerMove()
      {
          while (true)
          {
              player.Translate(moveForward * moveSpeed * Time.deltaTime);
              if (Mathf.Abs(transform.localPosition.x) > pad.rect.width * 0.2f)
                  player.Rotate(moveRotate * rotateSpeed * Time.deltaTime);
              yield return null;
          }
      }


As you can see, I only calculated the x local position and for y I just pass the value 0f. Hope this helps

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 stefanvlad199403 · Feb 28, 2021 at 10:03 AM 0
Share

Omg, it's working! Thank you! :)

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

206 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

Related Questions

Using Right Joystick to Select Weapon in Weapon Wheel 1 Answer

Character Movement w/ Both Joysticks 0 Answers

UI Navigation with gamepad 7 Answers

Rotating a ship with joystick and keeping camera centered. 0 Answers

Joystick unusual behavior on axis with almost the same script as W A S D 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