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

问题描述 投票:0回答:1

到目前为止,我已经指向光标并且每当我按下给定按钮时在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;         
    }


}
}
c# unity3d 2d sprite
1个回答
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;         
}

} }

© www.soinside.com 2019 - 2024. All rights reserved.