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 orvarjayway · Oct 10, 2016 at 07:37 AM · spritemobilecanvasjoystickscaling

Virtual joystick and canvas scaler

Hi

I'm very new to Unity and have started with the rollerball tutorial. I am trying to make it work on my mobile (android) and have followed this tutorial (https://www.youtube.com/watch?v=uSnZuBhOA2U) to implement a virtual joystick and it works very well except that it is really small on my device (high resolution) and a lot bigger on my daughters (low resolution) phone. After some searching I found the Canvas Scaler and it has a "scale with screen size" that scales everything on canvas depending on screen size. It seems to be what I was after but for some reason it screws up the virtual joystick, All I can do now is move in one direction. I think it probably has something to do with the ScreenPointToLocalPointInRectangle but I don't understand anything in that code (from the virtual joysick) so I don't know what is causing the problem. I hope that someone who is familiar with coding in unity can point out whats wrong or help me find a better way of doing it (maybe there is some "standard" way of doing virtual joysticks?)

Thanks!

alt text

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {
 
     private Image joystick_bg;
     private Image Joystick_nob;
     private Vector3 inputVector;
 
     private void Start() {
         joystick_bg = GetComponent<Image> ();
         Joystick_nob = transform.GetChild (0).GetComponent<Image> ();
     }
 
     public virtual void OnDrag(PointerEventData ped) {
         Vector2 pos;
         if(RectTransformUtility.ScreenPointToLocalPointInRectangle(joystick_bg.rectTransform, ped.position, ped.pressEventCamera, out pos)) {
             pos.x = (pos.x / joystick_bg.rectTransform.sizeDelta.x);
             pos.y = (pos.y / joystick_bg.rectTransform.sizeDelta.y);
             inputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
             Debug.Log (inputVector);
             inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
 
             Joystick_nob.rectTransform.anchoredPosition = new Vector3(inputVector.x * (joystick_bg.rectTransform.sizeDelta.x / 3),
                 inputVector.z * (joystick_bg.rectTransform.sizeDelta.y /3));
         }
         
 
     }
     public virtual void OnPointerDown(PointerEventData ped) {
         OnDrag (ped);
     }
     public virtual void OnPointerUp(PointerEventData ped) {
         inputVector = Vector3.zero;
         Joystick_nob.rectTransform.anchoredPosition = Vector3.zero;
     }
     public float Horizontal() {
         if (inputVector.x != 0)
             return inputVector.x;
         else
             return Input.GetAxis ("Horizontal");
     }
     public float Vertical() {
         if (inputVector.z != 0)
             return inputVector.z;
         else
             return Input.GetAxis ("Vertical");    
     }
 }


canvas-scaler.png (25.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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Canvas Scaling! 0 Answers

Scene loading incorrectly 1 Answer

[Simple] All sprites are showing, Canvas UI 0 Answers

2D optimisation - mobile devices 0 Answers

Many sprites need to load on mobile devices in unity3D 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