Search Unity

Direction between mousedown and mouseup

Discussion in 'Scripting' started by manfred79, Apr 22, 2014.

  1. manfred79

    manfred79

    Joined:
    Apr 22, 2014
    Posts:
    3
    HI Everyone,

    I'm trying to get the direction between mousedown and mouseup, for projectile. But i'm getting my z direction always zero, whenever i try to throw my object it goes in y direction. Below is my script.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var speed :float = 1500;
    5. var startMousePos:Vector3;
    6. var endMousePos:Vector3;
    7. var direction:Vector3;
    8. var sphere : GameObject;
    9.  
    10. function Start () {
    11.    
    12. }
    13.  
    14. function OnMouseUp(){
    15.     endMousePos = Input.mousePosition;
    16.     direction = (endMousePos-startMousePos).normalized;
    17.     var ballClone :GameObject = Instantiate(sphere,this.transform.position,this.transform.rotation);
    18.     ballClone.rigidbody.AddForce(direction*speed);
    19. }
    20.  
    21. function OnMouseDown(){
    22.     startMousePos = Input.mousePosition;
    23. }
    24.  
    25. function Update () {
    26.  
    27. }
    any idea how to get z direction right.


    Regards

    Manfred
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. manfred79

    manfred79

    Joined:
    Apr 22, 2014
    Posts:
    3
    Hi thanks for the reply.

    I'm making 3d game
     
  4. manfred79

    manfred79

    Joined:
    Apr 22, 2014
    Posts:
    3
    Basically i want to make projectile by swiping of the finger in any direction.