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 Calum-McManus · Mar 11, 2014 at 02:18 PM · c#androidmobilejoystickngui

Android C# JoyStick Movement

So I currently have 2 JoySticks On the screen, they will follow each of my thumbs fine but I can't figure out how to limit there movement and make them move around the centre point while following my fingers when my thumb goes out goes off of it while playing (like everyone joy stick).

Please no asset packs or "you can use the unity mobile assets".

I want to make it from scratch so I can learn and remember how to do it!

 using UnityEngine;
 using System.Collections;
 
 public class JoyStick : MonoBehaviour
 {
 
     public bool JoyMoveIsTouched;
     public bool JoyTurnIsTouched;
 
     public GameObject JoyMove;
     public GameObject JoyTurn;
 
     public GameObject CenterMove;
     public GameObject CenterTurn;
 
     public float m_fMaxDistance;
 
     public Vector3 TurnPos;
     public Vector3 MovePos;
 
     [SerializeField] private Camera UICam;
     // Use this for initialization
     void Start ()
     {
         TurnPos = JoyTurn.transform.position;
         MovePos = JoyMove.transform.position;
     }
     
     // Update is called once per frame
     void Update () {
     
     JoyMoveIsTouched = false;
     JoyTurnIsTouched = false;
     Vector3 vCenterMove = CenterMove.transform.position;
     Vector3 vCenterTurn = CenterTurn.transform.position;
         foreach (var touch in Input.touches)
         {
             Ray ray = UICam.camera.ScreenPointToRay(touch.position);
             RaycastHit hit;
             if (JoyMove.collider.Raycast(ray, out hit, Mathf.Infinity))
             {
                 JoyMoveIsTouched = true;
                 Vector3 vHitPoint = new Vector3(hit.point.x, hit.point.y, 0);
                 if (Vector3.Distance(JoyMove.transform.position, MovePos) > 0.2f)
                 {
                     JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, MovePos, Time.deltaTime * 0.5f);
 
                 }
                 else
                 {
                     JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, vHitPoint,
                                                                      Time.deltaTime * 3);
                 }
             }
             else
             {
                 //JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, MovePos, Time.deltaTime * 7);
             }
             if(JoyTurn.collider.Raycast(ray, out hit, Mathf.Infinity))
             {
                 JoyTurnIsTouched = true;
                 Vector3 vHitPoint = new Vector3(hit.point.x, hit.point.y, 0);
                 if (Vector3.Distance(JoyTurn.transform.position, TurnPos) > 0.2f)
                 {
                    JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, TurnPos, Time.deltaTime*0.5f);
                     
                 }
                 else
                 {
                     JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, vHitPoint,
                                                                      Time.deltaTime*3);
                 }
                
                 
             }
            
             else
             {
                 //JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, TurnPos, Time.deltaTime * 7);
             }
         }
         
         if(Input.touchCount < 1)
         {
             JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, MovePos, Time.deltaTime * 7);
             JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, TurnPos, Time.deltaTime * 7);
             
         }
         
         
     }
 }
 

That's my current script but it really isn't very good xD

I've been programming in C# and using unity for years, but for some reason I'm finding this really hard :(

Thanks in advance! :)

EDIT: BTW! the sprites (the game objects in the script) are NGUI Sprites with colliders on!

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

20 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

Related Questions

Joystick movement and turning 3rd person(Static Camera) 0 Answers

XR Descriptor not found 1 Answer

How to check whether you have swiped upwards on a mobile device - c# 1 Answer

Soft turn with joystick 0 Answers

Android MP4 Video Playback 2 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