Search Unity

Zombiesmash style drag and throw required in 2D

Discussion in '2D' started by farazshahid, Oct 31, 2014.

  1. farazshahid

    farazshahid

    Joined:
    Oct 31, 2014
    Posts:
    1
    I need your help friends. I am creating a game in which i have to gameObjects are running in to the scene and they are stealing another gameobject (scoring item) and run back to where they had came from. I have to drag them and throw them away so that they can't reach the score items. I have been able to drag and throw but they look too much heavy. It takes too much effort to drag them and throw them. Please suggest me how can i make them launch seamlessly similar to zombiesmash game. I want to use Raycast approach for it but Raycasting mess up the dragging part of the game. Following is my code for it.

    Code (CSharp):
    1.  
    2. void Update ()
    3.         {  
    4.              
    5.                 if (!isFalling) {      
    6.                                 if (!isFoodPicked) {
    7.                                         Run (true, gameObject);
    8.                                 } else {
    9.                                         Run (false, gameObject);
    10.                                 }
    11.                         }
    12.          }
    13.              
    14. public void Run (bool inScene, GameObject target)
    15.         {
    16.                 if (inScene) {
    17.                         RunIn (target);
    18.                 } else {
    19.                         Runout (target);
    20.                 }
    21.         }
    22. private void RunIn (GameObject target)
    23.         {  
    24.                 target.rigidbody2D.velocity = new Vector2 (rigidbody2D.transform.localScale.x * -1, 0) * speed;
    25.                 target.transform.localScale = new Vector3 (1.5f, 1.5f, 1.5f);
    26.         }
    27.      
    28.      
    29.         private void Runout (GameObject target)
    30.         {
    31.                 rigidbody2D.velocity = new Vector2 (rigidbody2D.transform.localScale.x, 0) * -speed;
    32.                 transform.localScale = new Vector3 (-1.5f, 1.5f, 1.5f);
    33.                 if (target) {
    34.                        //move the stealing item with them
    35.                         target.rigidbody2D.velocity = new Vector2 (rigidbody2D.transform.localScale.x, 0) * -speed;
    36.                      
    37.                 }
    38.         }
    39.  
    40. void OnMouseDown ()
    41.         {
    42.                 startTime = Time.time;            
    43.                 pickStartPos = transform.position;
    44.             isFalling = true;
    45.                 screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
    46.                 offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    47.              
    48.                 rigidbody2D.isKinematic = true;
    49.            
    50.         }
    51. void OnMouseDrag ()
    52.         {
    53.                 Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);        
    54.                 Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint), offset;
    55.                 ;      
    56.                 transform.position = curPosition;
    57.                 anim = gameObject.GetComponent<Animator> ();
    58.                 if (anim) {
    59.                         anim.SetInteger ("Animstat", animStatDrag);
    60.                 }
    61.          
    62. void OnMouseUp ()
    63.         {
    64.                 Debug.Log ("Mouse UP is Hit");
    65.                 rigidbody2D.isKinematic = false;
    66.                 rigidbody2D.gravityScale= 10;
    67.                 pickThrowpos = transform.position;
    68.              
    69.                 force =  pickThrowpos- pickStartPos;    
    70.              
    71.              
    72.                 if (Mathf.Abs (pickStartPos.x) != Mathf.Abs (pickThrowpos.x)) {
    73.                         isThrown = true;
    74.                         if (Mathf.Abs (pickStartPos.x) > Mathf.Abs (pickThrowpos.x)) {
    75.                          
    76.                         }
    77.                 }
    78.                 if (isThrown) {
    79.                  
    80.                         rigidbody2D.velocity = force * speed;
    81.                      
    82.                         anim = gameObject.GetComponent<Animator> ();
    83.                         if (anim)
    84.                         {
    85.                                 anim.SetInteger ("Animstat", animStatDrop);
    86.                         }
    87.  
    88.                 }
    89.         }
    90.  
    91.         }
    92.