Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Why do I get different values for those 2 things?

Discussion in 'Scripting' started by Deleted User, Aug 24, 2014.

  1. Deleted User

    Deleted User

    Guest

    This part:

    Code (CSharp):
    1.                 for (int i = 0; i < touchPoints[fingerId].Count; i++) {
    2.                         newPoints.Add (new Vector2 ((touchPoints [fingerId] [i].x - minX) / scaleX, (touchPoints [fingerId] [i].y - minY) / scaleY));
    3.  
    4.                         Transform t = Instantiate (scaledPoint, Camera.main.ScreenToWorldPoint (newPoints [i]), Quaternion.identity) as Transform;
    5.                         t.Translate (t.position.x, t.position.y, 0);
    6.                         Debug.Log ("Finger " + fingerId + " - t.Position: " + t.position);
    7.                         Debug.Log ("Finger " + fingerId + " - newPoints[" + i + "]: " + newPoints [i]);
    8.                         Debug.Log ("Finger " + fingerId + " - newPoints[" + i + "] ScreenToWorld: " + Camera.main.ScreenToWorldPoint (newPoints [i]));
    9.                      
    10.                 }
    The t.position and the "Camera.main.ScreenToWorldPoint (newPoints )" give me 2 different values. Why??

    Code (CSharp):
    1.         private List<Vector2> Scale (int fingerId)
    2.         {
    3.                 Debug.Log ("Finger " + fingerId + " - Points to scale:" + touchPoints [fingerId].Count);
    4.  
    5.                 List<Vector2> newPoints = new List<Vector2> ();
    6.                 float minX = float.MaxValue, minY = float.MaxValue, maxX = float.MinValue, maxY = float.MinValue;
    7.                
    8.                 for (int i = 0; i < touchPoints[fingerId].Count; i++) {
    9.                         if (touchPoints [fingerId] [i].x < minX) {
    10.                                 minX = touchPoints [fingerId] [i].x;
    11.                         }
    12.                         if (touchPoints [fingerId] [i].y < minY) {
    13.                                 minY = touchPoints [fingerId] [i].y;
    14.                         }
    15.                         if (touchPoints [fingerId] [i].x > maxX) {
    16.                                 maxX = touchPoints [fingerId] [i].x;
    17.                         }
    18.                         if (touchPoints [fingerId] [i].y > maxY) {
    19.                                 maxY = touchPoints [fingerId] [i].y;
    20.                         }
    21.                 }
    22.                
    23.                 float scaleX = (maxX - minX) / RESCALE, scaleY = (maxY - minY) / RESCALE;
    24.                
    25.                 Debug.Log ("Finger " + fingerId + " - minX = " + minX + " - minY = " + minY + " maxX = " + maxX + " maxY = " + maxY);
    26.                 Debug.Log ("Finger " + fingerId + " - scaleX = " + scaleX + " - scaleY = " + scaleY);
    27.                
    28.                 for (int i = 0; i < touchPoints[fingerId].Count; i++) {
    29.                         newPoints.Add (new Vector2 ((touchPoints [fingerId] [i].x - minX) / scaleX, (touchPoints [fingerId] [i].y - minY) / scaleY));
    30.  
    31.                         Transform t = Instantiate (scaledPoint, Camera.main.ScreenToWorldPoint (newPoints [i]), Quaternion.identity) as Transform;
    32.                         t.Translate (t.position.x, t.position.y, 0);
    33.                         Debug.Log ("Finger " + fingerId + " - t.Position: " + t.position);
    34.                         Debug.Log ("Finger " + fingerId + " - newPoints[" + i + "]: " + newPoints [i]);
    35.                         Debug.Log ("Finger " + fingerId + " - newPoints[" + i + "] ScreenToWorld: " + Camera.main.ScreenToWorldPoint (newPoints [i]));
    36.                        
    37.                 }
    38.  
    39.                 Debug.Log ("Finger " + fingerId + " - Points Scaled: " + newPoints.Count);
    40.  
    41.                 return newPoints;
    42.         }
    EDIT: SOLVED BY JOE STROUT
     
    Last edited by a moderator: Oct 24, 2014
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I'd say it's because, after you instantiate t at Camera.main.ScreenToWorldPoint(newPoints), you immediately translate it (by its own x and y position). Why would it be the same after you move it somewhere else?

    But perhaps I've missed the point...
     
    Deleted User likes this.