Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一变换。向上不旋转_C#_Unity3d_Transform - Fatal编程技术网

C# 统一变换。向上不旋转

C# 统一变换。向上不旋转,c#,unity3d,transform,C#,Unity3d,Transform,我试图创建一些基本的游戏AI逻辑,让一个玩家运行,避免另一个。这很好,但我已经这么做了,我现在正在寻找防守队员(以前是静态的)来追赶跑步者。我为此奋斗了很长一段时间,但发现了transform.up是我一直在寻找的魔法,因为它使用玩家Y(绿色轴)移动并使用旋转 在我的2d飞机上,亚军在页面上跑得很好,但我的防守队员在跑,这就是我的问题所在。当我将字符在Z轴上旋转180度时,我预计transform.up会沿着页面向下移动。我发现transform.up仍然沿着页面向上移动,我不确定我遗漏了什么

我试图创建一些基本的游戏AI逻辑,让一个玩家运行,避免另一个。这很好,但我已经这么做了,我现在正在寻找防守队员(以前是静态的)来追赶跑步者。我为此奋斗了很长一段时间,但发现了transform.up是我一直在寻找的魔法,因为它使用玩家Y(绿色轴)移动并使用旋转

在我的2d飞机上,亚军在页面上跑得很好,但我的防守队员在跑,这就是我的问题所在。当我将字符在Z轴上旋转180度时,我预计transform.up会沿着页面向下移动。我发现transform.up仍然沿着页面向上移动,我不确定我遗漏了什么

要移动播放器,我将执行以下操作(在Update()中):

为了尝试调试该问题,我在Start()中添加了以下内容:

//调试:显示位置指示器;
如果(showPositionIndicators==true){
Dictionary posIndicators=newdictionary();
posIndicators.Add(“Up”,transform.Up);
posIndicators.Add(“Rt”,transform.right);
posIndicators.Add(“Dn”,-transform.up);
posIndicators.Add(“Lt”,-transform.right);
posIndicators.Add(“Fd”,transform.forward);
posIndicators.Add(“Bk”,-transform.forward);
Vector3 scaleLocPos=新的Vector3(10f,10f,10f);
dbgposid=新列表();
foreach(posIndicators中的KeyValuePair posIndicator){
GameObject t=实例化(预置绑定、转换);
t、 name=“Pos_”+posIndicator.Key;
t、 transform.localPosition=Vector3.Scale(posIndicator.Value,scaleLocPos);
t、 GetComponent().text=posIndicator.Key;
dbgposid.Add(t);
}
}
这似乎表明transform.up确实“落后”,与跑步者相同。我错过了一些简单的东西吗?这是有意的吗?我如何让两名球员相对于各自的轮换向前移动(即两人都朝中间跑)


[

您正在使用
transform.up
作为
t.transform.localPosition=Vector3.Scale(posIndicator.Value,scaleLocPos);
中的局部空间向量,但是
transform.up
给出了世界空间中的变换局部上向量。它类似于
transform.TransformDirection(Vector3.up);

旋转180度的对象的局部空间上方向向量在世界空间中为下方向。这一部分是正确的。但当您将其用作旋转对象的局部位置时,由于对象旋转,该局部空间位置设置为向下,并且将再次在世界空间中为上方向

这就是为什么“向上”调试文本应该是颠倒的,但世界空间是向上的,因为在对象的局部空间中,它是向下的

t.transform.localPosition
赋值中使用
Vector3.up
(和其他方向),因为引用已引用本地空间位置


由于默认情况下
transform.Translate()
使用局部空间,因此也会出现同样的问题。您的局部空间向上(获取时)已向下(在世界空间中),但您的局部空间向下转换已在世界空间中备份

使用
transform.Translate(Vector3.up*15f*Time.deltaTime)
(推荐),或相对于世界空间进行转换
transform.Translate(transform.up*15f*Time.deltaTime
,space.world
);


另一个潜在问题:


假设您的角色具有或将要具有物理元素,则不要在更新中移动,也不要使用变换。请在
FixedUpdate()
上使用
Rigidbody2D.MovePosition()
。存在
Rigidbody2D.position
,但这将覆盖其他物理效果,因此
MovePosition()建议使用

您正在使用
transform.up
作为
t.transform.localPosition=Vector3.Scale(posIndicator.Value,scaleLocPos);
中的局部空间向量,但是
transform.up
给出了世界空间中的变换局部上向量。它类似于
transform.TransformDirection(Vector3.up)的缩写;

旋转180度的对象的局部空间上方向向量在世界空间中为下方向。这一部分是正确的。但当您将其用作旋转对象的局部位置时,由于对象旋转,该局部空间位置设置为向下,并且将再次在世界空间中为上方向

这就是为什么“向上”调试文本应该是颠倒的,但世界空间是向上的,因为在对象的局部空间中,它是向下的

t.transform.localPosition
赋值中使用
Vector3.up
(和其他方向),因为引用已引用本地空间位置


由于默认情况下
transform.Translate()
使用局部空间,因此也会出现同样的问题。您的局部空间向上(获取时)已向下(在世界空间中),但您的局部空间向下转换已在世界空间中备份

使用
transform.Translate(Vector3.up*15f*Time.deltaTime)
(推荐),或相对于世界空间进行转换
transform.Translate(transform.up*15f*Time.deltaTime
,space.world
);


另一个潜在问题:


假设您的角色具有或将要具有物理元素,则不要在更新中移动,也不要使用变换。请在
FixedUpdate()
上使用
Rigidbody2D.MovePosition()
。存在
Rigidbody2D.position
,但这将覆盖其他物理效果,因此
MovePosition()建议使用

效果很好,令人烦恼的是,我确实有Vector3.up(和co),但在发现transform.up后为了保持一致性而对其进行了更改。如果我将transform.Translate更改为Vector3.up,则无法避免防御
transform.Translate(transform.up * 15f * Time.deltaTime);
// DEBUG: Show position indicators;
if (showPositionIndicators == true) {

    Dictionary<string, Vector3> posIndicators = new Dictionary<string, Vector3>();
    posIndicators.Add("Up", transform.up);
    posIndicators.Add("Rt", transform.right);
    posIndicators.Add("Dn", -transform.up);
    posIndicators.Add("Lt", -transform.right);
    posIndicators.Add("Fd", transform.forward);
    posIndicators.Add("Bk", -transform.forward);

    Vector3 scaleLocPos = new Vector3(10f, 10f, 10f);
    dbgPosInd = new List<GameObject>();
    foreach (KeyValuePair<string, Vector3> posIndicator in posIndicators) {

        GameObject t = Instantiate(prefabPosInd, transform);
    t.name = "Pos_" + posIndicator.Key;
        t.transform.localPosition = Vector3.Scale(posIndicator.Value, scaleLocPos);
        t.GetComponent<TextMeshProUGUI>().text = posIndicator.Key;
        dbgPosInd.Add(t);
    }
}