Skip to main content

How to create joystick in unity

Joystick in unity

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

public class Joystick : MonoBehaviour {
    public Transform player;
    public float speed = 5.0f;
    private bool touchStart = false;
    private Vector2 pointA;
    private Vector2 pointB;

    public Transform circle;
    public Transform outerCircle;

	// Update is called once per frame
	void Update () {
        if(Input.GetMouseButtonDown(0)){
            pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));

            circle.transform.position = pointA * -1;
            outerCircle.transform.position = pointA * -1;
            circle.GetComponent<SpriteRenderer>().enabled = true;
            outerCircle.GetComponent<SpriteRenderer>().enabled = true;
        }
        if(Input.GetMouseButton(0)){
            touchStart = true;
            pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
        }else{
            touchStart = false;
        }
        
	}
	private void FixedUpdate(){
        if(touchStart){
            Vector2 offset = pointB - pointA;
            Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
            moveCharacter(direction * -1);

            circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * -1;
        }else{
            circle.GetComponent<SpriteRenderer>().enabled = false;
            outerCircle.GetComponent<SpriteRenderer>().enabled = false;
        }

	}
	void moveCharacter(Vector2 direction){
        player.Translate(direction * speed * Time.deltaTime);
    }
}

Comments

Popular posts from this blog

AWSD controller with c#

      using   UnityEngine ;   using   System.Collections ;   using   System.Collections.Generic ;     [ RequireComponent  ( typeof ( Rigidbody ) ) ]   public   class  PlayerController  :  MonoBehaviour  {         public   float  movementSpeed  =  5 . 0f ;         void  Start  ( )   {                 }             void  Update  ( )   {           Rigidbody rigidbody  =  GetComponent < Rigidbody > ( ) ;             if ( Input . GetKey ( KeyCode . W ) )   {              transform . position   +=  Vector3 . forward   *  Time . deltaTime   *  movementSpeed ;           }           else   if ( Input . GetKey ( KeyCode . S ) )   {              rigidbody . position   +=  Vector3 . back   *  Time . deltaTime   *  movementSpeed ;           }           else   if ( Input . GetKey ( KeyCode . A ) )   {              rigidbody . position   +=  Vector3 . left   *  Time . deltaTime   *  movementSpeed ;           }           else   if ( Input . GetKey ( KeyCode . D ) )   {              rigidbody . p

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial gives a complete understanding of Java. This reference will take you through simple and practical approaches while learning Java Programming language.

  Java Tutorial Java - Home Java - Overview Java - Environment Setup Java - Basic Syntax Java - Object & Classes Java - Constructors Java - Basic Datatypes Java - Variable Types Java - Modifier Types Java - Basic Operators Java - Loop Control Java - Decision Making Java - Numbers Java - Characters Java - Strings Java - Arrays Java - Date & Time Java - Regular Expressions Java - Methods Java - Files and I/O Java - Exceptions Java - Inner classes Java Object Oriented Java - Inheritance Java - Overriding Java - Polymorphism Java - Abstraction Java - Encapsulation Java - Interfaces Java - Packages Java Advanced Java - Data Structures Java - Collections Java - Generics Java - Serialization Java - Networking Java - Sending Email Java - Multithreading Java - Applet Basics Java - Documentation Java Useful Resources Java - Questions and Answers Java - Quick Guide Java - Useful Resources Java - Discussion Java - Examples Selected Reading UPSC IAS Exams Notes Developer's Best Practice

Computing language for chapter 3 : The trigonometric function