Transform.left 不起作用 Unity [英] Transform.left doesnt works Unity

查看:39
本文介绍了Transform.left 不起作用 Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有人问过这个问题,但不知道如何解决这个问题.

I know this has been asked but can't figure out how to solve this.

if (this.gameObject.tag == "1Team"){
         
             unitGO.transform.position += transform.right * movementSpeed * Time.deltaTime;
 
          } else if (this.gameObject.tag == "2Team"){
                           
             unitGO.transform.position += transform.left * movementSpeed * Time.deltaTime;
 }

红色 transform.left 中的统一标记( Transform' 不包含 'left' 的定义),所以我不知道如何将我的角色向左移动.我试过使用 -Transform.right, Transform.right * -movementSpeed, new Vector3( - 1,0,0), unitGO.transform.position -= new Vector3(1,0,0) * movementSpeed * Time.deltaTime; 和其他选项没有得到我想要的运动.

Unity marks in red transform.left ( Transform' does not contain a definition for 'left' ), so I dont know how to move my character to the left. I have tried using -Transform.right, Transform.right * -movementSpeed, new Vector3( - 1,0,0), unitGO.transform.position -= new Vector3(1,0,0) * movementSpeed * Time.deltaTime; and other options without getting the movement I want.

如果我从想要移动的 GameObject 更改标签,它实际上会向右移动,所以我认为这与附加脚本无关

if I change the tag from the GameObject I want to move, it actually moves right, so I dont think it's anything related to attaching scripts

https://docs.unity3d.com/ScriptReference/Vector3-left.html

有什么建议吗?

谢谢

推荐答案

虽然 Vector3.left 存在,但对于 Transform 来说,只有 transform.right.

While Vector3.left exists, for Transform there is only transform.right.

这没有问题,因为要向左移动,您只需使用 -transform.right.

Which is no problem since for moving left you simply use -transform.right.

现在请注意,您使用 new Vector3 的尝试也不起作用,因为如果您使用它,您将沿着 Unity 的 X 轴在全局空间中移动......而不是在本地空间.

Now note that your intempts of using new Vector3 also didn't work since if you use that you move in global space along Unity's X axis .. not in local space.

现在有多种可能的解决方案:

Now there are multiple possible solutions:

为了在您当地的空间中移动,例如

For moving in your local space do e.g.

if (gameObject.CompareTag("1Team"))
{
    unitGO.transform.position += transform.right * movementSpeed * Time.deltaTime;
} 
else if (gameObject.CompareTag("2Team"))
{
    unitGO.transform.position -= transform.right * movementSpeed * Time.deltaTime;

    // Same as
    //unitGO.transform.position += -transform.right * movementSpeed * Time.deltaTime;
}

或者如果你想在 Unity 的全局 X 轴上移动,那么

Or if you wan to move in Unity's global X axis then

if (gameObject.CompareTag("1Team"))
{
    unitGO.transform.position += Vector3.right * movementSpeed * Time.deltaTime;
} 
else if (gameObject.CompareTag("2Team"))
{
    unitGO.transform.position += Vector3.left * movementSpeed * Time.deltaTime;

    // Same as
    //unitGO.transform.position -= Vector3.right * movementSpeed * Time.deltaTime;
}

或者,如果您确实想沿着您正在移动的对象的局部 X 轴移动,您可以使用 Translate,它默认使用移动对象的局部空间

Or if you actually rather wanted to move along the local X axis of the object you are moving you could rather use Translate which by default uses the local space of the moved object

if (gameObject.CompareTag("1Team"))
{
    unitGO.transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
} 
else if (gameObject.CompareTag("2Team"))
{
    unitGO.transform.Translate(Vector3.left * movementSpeed * Time.deltaTime);

    // Same as
    //unitGO.transform.Translate(-Vector3.right * movementSpeed * Time.deltaTime);
}


一般来说,最好使用 CompareTag== 如果有拼写错误则抛出错误


In general rather use CompareTag instead of == to throw an error if there is a typo

这篇关于Transform.left 不起作用 Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆