Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Increasing Sensitivity With Dragging Sprite

Discussion in 'Scripting' started by coder_for_life22, Aug 16, 2018.

  1. coder_for_life22

    coder_for_life22

    Joined:
    Dec 25, 2013
    Posts:
    46
    I am trying to drag a rigibody only the X axis. The code I have works fine! but I have found that the dragging does not have as swift as I like it.

    I am wondering how it is possible to increase the sensitivity with the dragging of the sprite so that it responds faster to the drag and seems more 'snappy'.

    Here' the code I am using right now to get the drag working on the X axis.

    Code (CSharp):
    1. // Update is called once per frame
    2.     void Update () {
    3.  
    4.              if (Input.GetMouseButtonDown(0))
    5.              {
    6.                  if(gameObject.GetComponent<Player>().startShooting == false){
    7.                      gameObject.GetComponent<Player>().startShooting = true;
    8.                      objManager.gm.startGame();
    9.                      mainMenu.removeMenu();
    10.                      gameScene.SetActive(true);
    11.                 }
    12.                      dragging = true;
    13.                      mouseStartPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, -3, 0));
    14.                      playerStartPos = gameObject.transform.position;
    15.              }
    16.              else if (Input.GetMouseButtonUp(0))
    17.              {
    18.                      dragging = false;
    19.              }
    20.  
    21.              if (dragging)
    22.              {
    23.  
    24.                      Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, -3, 0));
    25.  
    26.                    
    27.                           mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x*Time.deltaTime, -3, 0));
    28.                    
    29.                          Vector3 move = mousePos - mouseStartPos;
    30.  
    31.                     // gameObject.transform.position = playerStartPos + move;
    32.                      GetComponent<Rigidbody2D>().MovePosition(playerStartPos + move);
    33.                      
    34.              }
    35.          }
    36.      }
    37.  
     
  2. coder_for_life22

    coder_for_life22

    Joined:
    Dec 25, 2013
    Posts:
    46
    By the way, the drag of the sprite happens no matter where you drag the screen. So you don't have to actually to touch the sprite to drag it.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    From the docs:

     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Have you tried switching to FixedUpdate for your move calls?