如何让一个游戏对象成长为一个方向而不会在相反的方向上同等增长?

时间:2015-03-05 08:06:55

标签: c# unity3d 2d sprite

到目前为止,每当我按下给定按钮时,我都指向光标并在y轴上生长,问题是它朝光标方向增长50%,而相反方向增长50%。这是我到目前为止的代码:

using UnityEngine;
using System.Collections;

public class Tunge : MonoBehaviour {

int x = 0;
bool vis = false;
public GameObject tungen;

public Animator doge;


void Start()
{

}

// Update is called once per frame
void Update () {


    Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
    Vector3 dir = Input.mousePosition - pos;
    float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

    doge.SetFloat ("tungelength", x);
    if (vis == true) 
    {
        tungen.SetActive(true);
    }

    if (vis == false) 
    {
        tungen.SetActive(false);

    }

    if (Input.GetMouseButton(0)) 
    {
        vis = true;

        float flyt = 0.1f;
        tungen.transform.localScale += new Vector3 (0, flyt, 0);

        x++;
    }

    if (!Input.GetMouseButton (0) && x > 0) 
    {
        float flyt = 0.1f;
        tungen.transform.localScale += new Vector3 (0, -flyt, 0);

        x--;
    }

    if (x == 0) 
    {
        vis = false;         
    }


}
}

1 个答案:

答案 0 :(得分:1)

缩放时必须翻译对象:

我在代码中添加了一些代码,请查看

using UnityEngine;
using System.Collections;

public class Tunge : MonoBehaviour {

int x = 0;
bool vis = false;
public GameObject tungen;

public Animator doge;


void Start()
{

}
// Update is called once per frame
void Update () {


Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

doge.SetFloat ("tungelength", x);
if (vis == true) 
{
    tungen.SetActive(true);
}

if (vis == false) 
{
    tungen.SetActive(false);

}

if (Input.GetMouseButton(0)) 
{
    vis = true;

    float flyt = 0.1f;
    tungen.transform.localScale += new Vector3 (0, flyt, 0);


    // ADD THIS LINE:
    tungen.transform.position += new Vector3( 0, flyt / 2, 0 );


    x++;
}

if (!Input.GetMouseButton (0) && x > 0) 
{
    float flyt = 0.1f;
    tungen.transform.localScale += new Vector3 (0, -flyt, 0);


    // ADD THIS LINE:
    tungen.transform.position += new Vector3( 0, -flyt / 2, 0 );


    x--;
}

if (x == 0) 
{
    vis = false;         
}

} }