根据鼠标位置增加局部比例

时间:2018-12-18 13:19:33

标签: c# unity3d

我正在尝试做一些不适合我的事情。我希望根据鼠标在y轴上的位置缩放对象。

void Update()
{
    var mPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    mPosition.z = 0;
    target.transform.position = mPosition;
    float scaleValue = Mathf.Round(mPosition.y);
    target.transform.localScale = new Vector3(target.transform.localScale.x + scaleValue / 50f, target.transform.localScale.y + scaleValue / 50f, 0f);
    if (Input.GetMouseButtonDown(0) && canShoot)
    {
        Fire();
    }
}

问题在于,一旦游戏开始,对象(目标)就会稳定增长。我只想做某种效果,当我将慕斯向上移动时,对象会缩小,当它向下移动时,对象会长。我错了,谢谢!!!

1 个答案:

答案 0 :(得分:0)

问题是您要在每个帧上添加本地比例。您应该按照以下方式进行操作:

Vector3 startScale;
void Awake(){
    startScale =   target.transform.localScale;
}
void Update()
{
    var mPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    mPosition.z = 0;
    target.transform.position = mPosition;
    float scaleValue = Mathf.Round(mPosition.y);
    // you probably need to change the scaleValue to make it make more sense
    target.transform.localScale =startScale  * scaleValue;
    if (Input.GetMouseButtonDown(0) && canShoot)
    {
        Fire();
    }
}