• 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 missingscenestudios · Oct 24, 2020 at 07:20 PM · mobileunity 2djoystickmobile devicesclamp

Show Joystick on the left part of the screen only

I made a joystick script and the joystick works perfectly(unity mobile) so the joystick shows up where you tap on the screen, but the problem is that is shows up anywhere on the screen, and I need it to show up on the left side only.

 using System.Collections;
 using System.Collections.Generic;
 
 using UnityEngine;
 using UnityEngine.Rendering;
 using UnityEngine.UI;
 
 
 public class NewBehaviourScript : MonoBehaviour
 {    
     public Transform player;
     private bool Touch;
     private float speed = 10f;
     private Vector2 A;
     private Vector2 B;
     public SpriteRenderer In2;
     public SpriteRenderer Out2;
     bool plsWork = false;
     float pls;
 
     public Transform Out;
     public Transform In;
 
     // Update is called once per frame
     void Update()
     {           
         if (Input.GetMouseButtonDown(0))
         {
             A = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
 
             Out.transform.position = A * 1;
             In.transform.position = A * 1;
             Out2.enabled = true;
             In2.enabled = true;
         }
        
         if(Input.GetMouseButtonDown(0))
         {
             Debug.Log("check");
         }
 
         if (Input.GetMouseButton(0))
         {
             Touch = true;
             B = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
 
         }
         else
         {
             Touch = false;
         }
     }

     private void FixedUpdate()
     {
         if (Touch)
         {
             Vector2 offset = A - B;
             Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
             moveCharacter(direction * -1);
             In.transform.position = new Vector2(A.x + -direction.x, A.y + -direction.y) * 1;
         }
         else
         {
             Out2.enabled = false;
             In2.enabled = false;
         }
     }
 
     void moveCharacter(Vector2 direction)
     {
             player.Translate(direction * speed * Time.deltaTime);           
     }       
 }
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 busedagidir · Oct 24, 2020 at 07:40 PM

You can use canvas for this. When you create a canvas in your hierarchy and in the inspector : "Canvas Scaler (Script) > UI Scale Mode choose "Scale with screen size and set x and y there. After that add that canvas a joystick image and set its position where ever you want. And final step you can add your script to this image I hope I could be able to undestand your question and help you :)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

209 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

Related Questions

EventSystem.IsPointerOverGameObject doesn't work on virtual joystick (mobile) 0 Answers

Change the position of the Joystick by tap 2 Answers

google play services build error 0 Answers

Unity UI - layout fixed number of buttons 1 Answer

Mobile game not starting,Unity mobile game not loading scene after menu 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges