Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drag the object only if there is a touch upon

Discussion in 'Scripting' started by Vinit, Jun 2, 2011.

  1. Vinit

    Vinit

    Joined:
    May 21, 2011
    Posts:
    21
    I am having issue with raycast function in order to drag an object when touched on object on that exact location of screen. I am using the following code :

    for (var i : int = 0; i < Input.touchCount; ++i)
    {
    if(Input.touchCount > 0)
    {
    var ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
    var hit : RaycastHit;
    if (Physics.Raycast(ray,hit))
    {
    if (hit.collider.gameObject.tag == "BallSpawnPoint")
    {
    tilt.x = -Input.GetTouch(i).deltaPosition.x;
    tilt.z = -Input.GetTouch(i).deltaPosition.y;

    transform.Translate(tilt.x * speed * Time.deltaTime,tilt.y,tilt.z * speed * 20 * Time.deltaTime);
    }
    }
    }
    }


    This script is applied to BallSpawnPoint Object which is used as primary object in game that is followed by other object in game.

    Problem : Since the ray focuses almost on the entire scene through camera, so any touch on any portion of camera viewing angle, is assumed that ray hits the BallSpawnPoint collider, and therefore the touch drags the BallSpawnPoint which is not desirable.
    Requirement for game : BallSpawnPoint object should only move when user exactly touches on the object So what can be other alternative.

    Please provide a solution to my above code
     
    Last edited: Jun 2, 2011
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Your approach looks more or less correct. Are you saying that no matter where you touch the screen, the object gets dragged anyway?
     
  3. Vinit

    Vinit

    Joined:
    May 21, 2011
    Posts:
    21
    Yes, that's correct. Since the ray from camera is widely spread through out the scene, it always hit the BallSpawnPoint object collider, and thereby object get dragged on touching anywhere on the screen.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Is the BallSpawnPoint collider always filling up the whole screen? (Maybe it is a large object parented to the camera, say?) It doesn't look as though this section of script is causing the problem. Perhaps you could post the whole script in case there are errors elsewhere. Other than that, it looks as though the BallSpawnPoint is always in front of the camera somehow.
     
  5. Vinit

    Vinit

    Joined:
    May 21, 2011
    Posts:
    21
    Thanks for your active initiation.

    To begin with, BallSpawnPoint Object is not a parented object but is does act as a object that is followed by other object which in this case is a bowling ball. For this kind of association of being followed by ball, code is in the entire different script and I believe this script cannot be a problem for the touch thing.
    And to answer your other question, yes, the ballSpawnPoint is always in front of camera. As far as the entire script is concerned, its the same above code under function update plus some variables defined outside of it. For convenience, I am pasting the entire script :

    Code (csharp):
    1.  
    2.  
    3. var tilt : Vector3 = Vector3.zero;
    4. var speed : float;
    5.  
    6. @script RequireComponent(Rigidbody)
    7.  
    8. function Update ()
    9. {
    10.  
    11.                    
    12.     for (var i : int = 0; i < Input.touchCount; ++i)
    13.     {
    14.         if(Input.touchCount > 0)
    15.         {
    16.             var ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
    17.             ray.origin.y = rigidbody.transform.position.y;
    18.             var hit : RaycastHit;
    19.                  //if (Physics.Raycast(ray,hit))
    20.             if(Physics.Raycast(ray.origin,-Vector3.forward,hit,3))
    21.                         {
    22.                            
    23.      
    24.                             if (hit.collider.gameObject.name == "BallSpawnPoint")
    25.                                 {
    26.                                     tilt.x = -Input.GetTouch(i).deltaPosition.x;
    27.                                     tilt.z = -Input.GetTouch(i).deltaPosition.y;
    28.    
    29.                                     if(transform.position.z > 18)           // To restrict the backward movement at certain point
    30.                                         {
    31.                                             transform.position.z = 18;
    32.                                             tilt.z = 0;
    33.                                         }
    34.                                
    35.                                     transform.Translate(tilt.x * speed * Time.deltaTime,tilt.y,tilt.z * speed * 20 * Time.deltaTime);
    36.                                    
    37.            
    38.                                 }
    39.            
    40.                         }
    41.             }  
    42.         }
    43.  
    44. }
    45.  
    46.  
    47.  
    I am banging my head on this script for long time to get the desired touch.
     
    Last edited: Jun 4, 2011
  6. CodeCody

    CodeCody

    Joined:
    Apr 27, 2010
    Posts:
    440
    Well I have no clue whats wrong with your script. But the fact that;
    Code (csharp):
    1.  
    2. var ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).pos ition); //the space inside of "pos ition"
    3.  
    Does not throw a error is quite mind boggling lol.

    P.s. You might also want to start using the CODE tags on the forum. It makes it a lot easier for people to read your code.
     
  7. Vinit

    Vinit

    Joined:
    May 21, 2011
    Posts:
    21
    Oops...that space happened to occur because there was no code tag around the script in the forum which I have applied now using edit option. Code is correct in term of syntax but still not getting the desirable solution. Any other way around ?
     
    Last edited: Jun 6, 2011