【问题标题】:game in Unity3d scaling objects with mouse in c sharpUnity3d 中的游戏用 c 中的鼠标缩放对象
【发布时间】:2013-09-26 17:16:32
【问题描述】:

我正在用 C# 在 unity3d 中制作游戏

我希望能够通过用鼠标左键单击对象来使对象变小,用鼠标右键单击对象来使对象变大。这段代码的问题是: 1. 它不允许缩小,除非它被放大 2. 如果有多个 obj,一旦它们被点击,它们都会受到影响。我尝试了几种不同的方法来做到这一点,我猜这与 resize bool 有关。非常感谢您的帮助

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);
            }
    }
    }
}

【问题讨论】:

  • 这已在统一网站 link 上为我解决了
  • 我没有足够的布朗尼积分来回答我自己的问题,但只是想在应得的地方给予赞扬。我会在 6 小时后回来亲自回答这个问题并将代码添加到线程中

标签: c# unity3d mouse transform scale


【解决方案1】:

使用 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);
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多