• 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 justasxz · Aug 12, 2020 at 11:33 PM · cameratransformjoystickrelative

Move JoyStick Together With Camera

Hey guys, I am making unity mobile game and I made a joystick to control my characters movement, the issue is my camera tracks the player, and whenever the camera moves joystick is left behind and after some time i can't even see it, so i was wondering is there a way to move my joystick together with the camera or relative to the camera so it's always at the same spot on the screen, here is my joystick code. I also have same joystick for shooting in another script if that matters, but its mostly a copy of this just for different side of screen.

         if (Input.touchCount > 0)
         {
             if (Outer == null && Input.touchCount > 1)
             {
                 for (int i = 0; i < Input.touchCount; i++)
                 {
                     if (Input.GetTouch(i).position.x <= Screen.width / 2)
                     {
                         t = Input.GetTouch(i);
                         break;
                     }
 
                 }
             }
             if (Input.touchCount == 1)
             {
                 t = Input.GetTouch(0);
             }
 
             for (int i = 0; i < Input.touchCount; i++)
             {
                 if (Outer != null && t.fingerId == Input.GetTouch(i).fingerId)
                 {
                     t = Input.GetTouch(i);
                 }
             }
 
             if (t.phase == TouchPhase.Began && t.position.x <= Screen.width / 2 && Outer == null && t.position.x != 0)
             {
                 A = Camera.main.ScreenToWorldPoint(t.position);
                 id = t.fingerId;
                 Outer = Instantiate(OuterPre, new Vector3(A.x, A.y), this.transform.rotation);
                 middle = Instantiate(middlePre, new Vector3(A.x, A.y), this.transform.rotation);
 
             }
 
             if (t.phase != TouchPhase.Ended && Outer != null && id == t.fingerId )
             {
                 Touched = true;
                 B = Camera.main.ScreenToWorldPoint(t.position);
             }
             else
             {
                 Touched = false;
                 t = new Touch();
                 Destroy(Outer);
                 Destroy(middle);
             }
 
         }
 
 
     }
 
 
     void FixedUpdate()
     {
         if (Touched)
         {
 
             Vector2 offset = B - A;
             Vector2 direction = Vector2.ClampMagnitude(offset, 1f);
             middle.transform.position = new Vector2(A.x + direction.x, A.y + direction.y);
             Move(direction);
         }
     }


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 justasxz · Aug 13, 2020 at 12:13 AM

Figured it out myself, leaving this if somebody need's it in the future. I set the Outer circle as a child to camera and now it moves relative to the camera, and after that i just set A vector equal to current Outer circle position, and middle circle adjusts itself.

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

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

Related Questions

How do you move the camera relative to the direction it is currently facing? 1 Answer

Changing the axes of a joystick based on the position of the camera? 1 Answer

Camera Relative Movement 2 Answers

Attaching camera to instantiated object 1 Answer

Camera following the player in 3D - in object hirarchy or through scripts? 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