Search Unity

Flick Sprite 2D

Discussion in 'Physics' started by keepStriving, Jan 23, 2016.

  1. keepStriving

    keepStriving

    Joined:
    Feb 1, 2015
    Posts:
    7
    I'm trying to flick a sprite which is a ball on touch though it doesn't seem to be responding.



    Here is my Script for the ball:
    Code (CSharp):
    1. public class Puck : MonoBehaviour {
    2.  
    3.     private const int VECTOR_COUNT = 5;
    4.     private int vectorIndex = 0;
    5.     private Vector3[] positionChanges;
    6.     private Vector3 prevMousePosition;
    7.     private float[] prevTimes;
    8.     private bool curDown = false;
    9.     private bool prevDown = false;
    10.  
    11.     void Start() {
    12.         prevMousePosition = Vector3.zero;
    13.         positionChanges = new Vector3[VECTOR_COUNT];
    14.         prevTimes = new float[VECTOR_COUNT];
    15.         for (int i = 0; i < VECTOR_COUNT; i++) {
    16.             positionChanges [i] = Vector3.zero;
    17.             prevTimes[i] = 0;
    18.         }
    19.     }
    20.  
    21.     void Update () {
    22.         if (curDown) {
    23.             // if still down just follow the mouse (finger)
    24.             Vector3 amountMoved = Camera.main.ScreenToWorldPoint (positionChanges[(vectorIndex - 1 + VECTOR_COUNT) % VECTOR_COUNT]) - Camera.main.ScreenToWorldPoint (new Vector2 (0, 0));
    25.             transform.position = transform.position + amountMoved;
    26.         } else if(prevDown) {
    27.             // just came up so calculate and set velocity
    28.             float totalTime = 0;
    29.             Vector3 totalDistance = Vector3.zero;
    30.             Vector3 velocity;
    31.             for(int i = 0; i < VECTOR_COUNT; i++) {
    32.                 totalTime += prevTimes[i];
    33.                 totalDistance += positionChanges[i];
    34.             }
    35.             velocity = totalDistance / totalTime;
    36.             velocity = Camera.main.ScreenToWorldPoint (velocity) - Camera.main.ScreenToWorldPoint (Vector3.zero);
    37.             GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity;
    38.         }
    39.  
    40.         positionChanges [vectorIndex] = Input.mousePosition - prevMousePosition;
    41.         prevTimes [vectorIndex] = Time.deltaTime;
    42.         vectorIndex = (vectorIndex + 1) % VECTOR_COUNT;
    43.  
    44.         prevDown = curDown;
    45.         prevMousePosition = Input.mousePosition;
    46.  
    47.         //--------------------------------------------------------------------------------------------------------------
    48.          if (!curDown) {
    49.              Vector2 temp = GetComponent<Rigidbody2D>().velocity;
    50.              if((transform.position.y > Camera.main.orthographicSize && GetComponent<Rigidbody2D>().velocity.y > 0) ||
    51.                  (transform.position.y < -Camera.main.orthographicSize && GetComponent<Rigidbody2D>().velocity.y < 0))
    52.                  temp.y *= -1;
    53.              if((transform.position.x > Camera.main.orthographicSize * Camera.main.aspect && GetComponent<Rigidbody2D>().velocity.x > 0) ||
    54.                 (transform.position.x < -Camera.main.orthographicSize * Camera.main.aspect && GetComponent<Rigidbody2D>().velocity.x < 0))
    55.                  temp.x *= -1;
    56.              GetComponent<Rigidbody2D>().velocity = temp;
    57.          }
    58.          //-------------------------------------------------------------------------------------------------------------
    59.     }
    60.  
    61.     void OnMouseDown()    {
    62.         print ("down");
    63.         curDown = true;
    64.         GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    65.     }
    66.  
    67.     void OnMouseUp()    {
    68.         print ("up");
    69.         curDown = false;
    70.     }
    71. }