为什么此代码无法更改bool在更新时移至true? Obstacle.Move将bool移动到true,但不在Update()上。也许这是一个简单的问题,但我无法解决这个问题。谢谢你的回答。
PlayerMove脚本:
Vector3 pos;
public float speed;
Obstacle obstacle;
void Start () {
obstacle = new Obstacle();
pos = transform.position;
}
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow))
pos.x -= speed;
if (Input.GetKeyDown(KeyCode.RightArrow))
pos.x += speed;
if (Input.GetKeyDown(KeyCode.UpArrow))
pos.y += speed;
if (Input.GetKeyDown(KeyCode.DownArrow))
pos.y -= speed;
transform.position = pos;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("side_collider"))
print("side");
else if (col.CompareTag("obstacle"))
{
obstacle.Move(true);
print("obstacle");
}
障碍脚本:
Vector3 pos;
public GameObject player;
bool move = false;
void Start () {
pos = transform.position;
}
public bool Move(bool mo)
{
if(mo)
move = true; // move <- true
return move;
}
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow))
move = false;
if (move) // but here is move <- false
transform.localPosition = player.transform.position;
}