2016-03-21 56 views
1

ピンチズーム用のこのスクリプトは見つかりましたが、これはカメラのズームインとズームアウトのためのスクリプトです。オブジェクトがより効果的に機能する方法を教えてください。私はキューブをスケールしたいだけで、キューブだけが同時に何もスケーリングされません。球体ならば球体のみに感謝します。それはせずに動作しません - オブジェクト衝突型加速器が取り付けられていなければならない: はここのコードです:ピンチズームで個々のオブジェクトを拡大縮小する方法

using UnityEngine; 
{ 
public float perspectiveZoomSpeed = 0.5f;  // The rate of change of the field of view in perspective mode. 
public float orthoZoomSpeed = 0.5f;  // The rate of change of the orthographic size in orthographic mode. 
void Update() 
{ 
    // If there are two touches on the device... 
    if (Input.touchCount == 2) 
    { 
     // Store both touches. 
     Touch touchZero = Input.GetTouch(0); 
     Touch touchOne = Input.GetTouch(1); 

     // Find the position in the previous frame of each touch. 
     Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; 
     Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; 

     // Find the magnitude of the vector (the distance) between the touches in each frame. 
     float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; 
     float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; 

     // Find the difference in the distances between each frame. 
     float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; 

     // If the camera is orthographic... 
     if (camera.isOrthoGraphic) 
     { 
      // ... change the orthographic size based on the change in distance between the touches. 
      camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed; 

      // Make sure the orthographic size never drops below zero. 
      camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f); 
     } 
     else 
     { 
      // Otherwise change the field of view based on the change in distance between the touches. 
      camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed; 

      // Clamp the field of view to make sure it's between 0 and 180. 
      camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f); 
     } 
    } 
} 
} 
+1

このようなプロセスのロジックは次のようになります。1.マークされたオブジェクトに 'markedGameObject'のような変数のスケールを適用します(レイキャストとヒットを探します)。 2.あなたのコードを使って 'deltaMagnitudeDiff'を決定します。 3.コードから 'if -else'文全体を削除します。 4. 'markedGameObject.localScale = deltaMagnitudeDiff'のようなもので置き換えます。 5.必要に応じてスケーリングの速度を調整します。申し訳ありませんが、それをテストする手段はありませんが、あなたがそれを試してみるときに質問をしてください:) –

+0

あなたは正しいですが、私はあなたに本当に感謝しているコードを編集することができますスクリプトです。あなたの提案に感謝します:) –

答えて

0

は、[OK]を、ので、私はそれを適切ので、そこにおそらくいくつかのバグをテストすることはできませんが、ここでのコードのための一般的な考え方です:

重要なのはそれは

using UnityEngine; 
using System.Collections; 
public class ObjectScalling : MonoBehaviour { 

private GameObject selectedObject;  
//public GameObject gameobject; 
// Update is called once per frame 
void Update() { 

    if (Input.touchCount == 0) 
    { 
     Touch touch = Input.touches[0]; 
     Ray ray = Camera.main.ScreenPointToRay(touch.position); 
     RaycastHit hit; 

     if (Physics.Raycast(ray, out hit, 100f)) 
     { 
      selectedObject = hit.collider.gameObject; 
     } 
    } 
    if (Input.touchCount == 2) 
    { 
     // Store both touches. 
     Touch touchZero = Input.GetTouch(0); 
     Touch touchOne = Input.GetTouch(1); 

     // Find the position in the previous frame of each touch. 
     Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; 
     Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; 

     // Find the magnitude of the vector (the distance) between the touches in each frame. 
     float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; 
     float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; 

     // Find the difference in the distances between each frame. 
     float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; 

     selectedObject.transform.localScale = new Vector3(deltaMagnitudeDiff , deltaMagnitudeDiff , deltaMagnitudeDiff); 

    } 
} 
} 

それはおそらくいくつかのバグが含まれていますが、一般的に、あなたはすべてが、この行を除いて、あなたのUpdate()に入るもちろんその:)での作業を開始することがあります。deltaMagnitudeDiff

+0

ありがとう私は後でそれをテストします:) –

+0

THanksは答えを受け入れるために、しかし多分あなたはそれを最初にテストする必要があります:) –

+0

ええ、私はそれをオブジェクトにこのスクリプトをドラッグしても何も起こらないタッチスクリーンで –

関連する問題