游戏在 Unity3d 中使用鼠标在 c 尖锐中缩放对象

本文关键字:缩放 对象 Unity3d 游戏 鼠标 | 更新日期: 2023-09-27 18:33:00

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

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

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

游戏在 Unity3d 中使用鼠标在 c 尖锐中缩放对象

使用UnityEngine;使用系统集合;

公共类 规模 : 单行为 {

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

}