2013-09-26 95 views
0

我在C#中做一個遊戲在unity3d鼠標對象遊戲Unity3d縮放在C尖銳

我希望能夠使對象較小通過點擊它與鼠標左鍵和更大的帶鼠標右鍵。這個代碼存在的問題是:1.除非被放大,否則它不允許縮小。2.如果有多個objs,它們一旦被點擊就會受到影響。我已經嘗試了幾種不同的方式來做到這一點,我猜這是與調整大小的布爾。您的幫助非常感謝

using UnityEngine; 
using System.Collections; 

public class Scale : MonoBehaviour 
{ 
    public GameObject obj; 

    private float targetScale; 
    public float maxScale = 10.0f; 
    public float minScale = 2.0f; 

    public float shrinkSpeed = 1.0f; 

    private bool resizing = false; 


void OnMouseDown() 
    { 
     resizing = true; 

    } 

    void Update() 
    { 
     if (resizing) 
     { 
      if (Input.GetMouseButtonDown(1)) 
      { 
       targetScale = maxScale; 


      } 
      if (Input.GetMouseButtonDown(0)) 
      { 
       targetScale = minScale; 


      } 

      obj.transform.localScale = Vector3.Lerp(obj.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed); 

      Debug.Log(obj.transform.localScale); 

      if (obj.transform.localScale.x == targetScale) 
      { 
      resizing = false; 
       Debug.Log(resizing); 
      } 
    } 
    } 
} 
+0

這是解決對我來說是統一的網站[鏈接](http://answers.unity3d.com/questions/543880/scaling-上objects-in-c.html) – lollie009

+0

我沒有足夠的布朗尼分數來回答我自己的問題,但只是想給出它應該到的地方。我會在6小時後回來自己回答這個問題,並將代碼添加到線程中 – lollie009

回答

0

using UnityEngine;使用System.Collections的 ;

公共類規模:MonoBehaviour {

public float maxScale = 10.0f; 
public float minScale = 2.0f; 
public float shrinkSpeed = 1.0f; 

private float targetScale; 
private Vector3 v3Scale; 

void Start() { 
    v3Scale = transform.localScale; 
} 

void Update() 
{ 
    RaycastHit hit; 
    Ray ray; 

    if (Input.GetMouseButtonDown (0)) { 
    ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    if (Physics.Raycast(ray, out hit) && hit.transform == transform) { 
     targetScale = minScale; 
     v3Scale = new Vector3(targetScale, targetScale, targetScale); 
    } 

    } 

    if (Input.GetMouseButtonDown (1)) { 
    ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    if (Physics.Raycast(ray, out hit) && hit.transform == transform) { 
     targetScale = maxScale; 
     v3Scale = new Vector3(targetScale, targetScale, targetScale); 
    } 
    } 

    transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime*shrinkSpeed); 
} 

}