青青发财树 2022-09-13 17:48 采纳率: 0%
浏览 24

update函数的问题

img


为什么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);
}

}

  • 写回答

2条回答 默认 最新

  • 陈言必行 Unity领域优质创作者 2022-09-13 18:14
    关注

    看了好几遍,没看出逻辑有什么问题,建议重启一下再试试

    评论

报告相同问题?

问题事件

  • 创建了问题 9月13日

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办