无法将“unityengine.vector3”类型转换为“浮动”

时间:2016-05-16 16:44:19

标签: c# unity3d

如果相机不在停止的地方,我试图让相机移回播放器,但我一直在标题中收到错误。这是我的代码:

public float minPos = 0.3234783f;
public float maxPos = 40f;

Vector3 tempPos;
public float playerPos;

private void Update() 
{
    playerPos = GameObject.Find ("Player").transform.position;
    tempPos   = transform.position;
    tempPos.z = -10f;

    if (transform.position.x < minPos)
         tempPos.x = minPos;
    else tempPos.x = playerPos;

    transform.position = tempPos;
}

我很擅长编码,如果这是一个非常愚蠢的错误,那就很抱歉。

2 个答案:

答案 0 :(得分:2)

您的问题在playerPos = GameObject.Find("Player").transform.position;

transform.position; Vector3不是floatplayerPos定义为float,因此您无法将Vector3保存到float

更改

public float playerPos;

public Vector3 playerPos;

并且还要改变

tempPos.x = playerPos;


 tempPos.x = playerPos.x;

答案 1 :(得分:0)

问题是这一行,IDE应该告诉你:

playerPos = GameObject.Find ("Player").transform.position;

playerPosfloat类型,但position属性类型为Vector3,并且它们之间没有转换。您可以将playerPos变为Vector3来修复它并相应地调整您的代码。