为什么update 里的canMove变量和脚本里的不能不同步 ?外面的变true了,update里还是false?希望是按键以后能执行update里的函数(按键事件看起来应该是正常的),望解答。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardBehavior : MonoBehaviour
{
private Vector2 primitivePos;
private Vector2 primitiveRot;
private bool canMove;
private Vector2 targetPos;
private float distance;
void Start()
{
distance = 0.4f;
canMove = false;
primitivePos = transform.position;
targetPos = new Vector2(0.7f, 0);
}
void Update()
{
Debug.Log("Update前是" + canMove);
if (canMove)
{
MoveTo(targetPos, distance);
}
}
public void MoveTo(Vector2 targetPos, float dis)
{
if (Vector2.Distance(transform.position, targetPos) > dis)
{
transform.Translate(Vector2.right * Time.deltaTime * 3);
Debug.Log("MoveTo过程中是" + canMove);
}
else
{
canMove = false;
Debug.Log("Move结束是" + canMove);
//等待是攻击还是返回
Invoke("GoBack", 0.1f);
}
}
private void GoBack()
{
// canMove = true;
// targetPos = primitivePos;
// distance = 0.01f;
transform.position = primitivePos;
canMove = true;
Debug.Log("GoBack后是" + canMove);
}
public void OnAttackButton()
{
canMove = true;
Debug.Log("OnAtkBtn后是" + canMove);
}
}