Search Unity

mousePosition returning previous position

Discussion in 'Editor & General Support' started by Overkitty, Jun 7, 2009.

  1. Overkitty

    Overkitty

    Joined:
    May 31, 2009
    Posts:
    7
    I've already submitted a bug report for this problem. After posting in the scripting section awhile back, it appears this is unexpected behavior. Hope its not too much of a problem that I am posting here too. If I can't figure out this problem soon I want to switch ideas since I am running the trial version. I want to get something presentable out before that time runs out.

    In this prototype for a Gauntlet-style game, clicking the mouse inside the game causes the player to shoot a fireball towards where the mouse clicked. The problem is that Input.mousePosition is returning the previous position of the mouse where I had clicked "Fire1". If I click "Fire1" a second time in the same position it will then shoot a fireball to the correct position because the previous position was the same.

    Code (csharp):
    1. var fireball : Transform;
    2.  
    3. function LateUpdate ()
    4. {
    5.     if (Input.GetButtonDown ("Fire1"))
    6.     {
    7.         // Construct a ray from the current mouse coordinates
    8.         var rayHit : RaycastHit;
    9.         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    10.        
    11.         if (Physics.Raycast (ray, rayHit))
    12.         {
    13.             // Get the point in space where the ray hit
    14.             // Set the y value to that of this object so that its height is equal
    15.             var clickPoint = rayHit.point;
    16.             clickPoint.y = transform.position.y;
    17.        
    18.             // Get the normalized ray from this object to the click point
    19.             rayHit.point.y = clickPoint.y;
    20.             var rayToClickPoint = rayHit.point - transform.position;
    21.             rayToClickPoint.Normalize();
    22.            
    23.             // Create a particle if hit
    24.             Instantiate (fireball,transform.position, transform.rotation);
    25.            
    26.             var fireballScript = fireball.GetComponent(ProjectileScript);
    27.             fireballScript.velocity = rayToClickPoint;
    28.             fireballScript.speed = 7.0;
    29.             fireballScript.lifetime = 3.0;
    30.         }
    31.     }
    32. }
    I've tried Update, LateUpdate, and FixedUpdate. They all provide the same results.

    I also attached the built project if you want to check out the problem.

    Here is a link to the fire post in Scripting: http://forum.unity3d.com/viewtopic.php?t=25568
     

    Attached Files: