使用ASTAR路径规划时,尝试在移动不同方向时翻转敌人的精灵出现问题。

huangapple go评论65阅读模式
英文:

Problem while trying to flip the sprite of an enemy when moving in a different direction using ASTAR pathfinding

问题

以下是您要的翻译内容:

I have a script that should flip the sprite left or right depending on the direction of the path it is taking but when I save the script I get the following error, "Cannot modify the return value of "AIBase.desiredVelocity" because it is not a variable".

我有一个脚本,根据路径的方向应该将精灵左右翻转,但当我保存脚本时,我收到以下错误信息:"无法修改"AIBase.desiredVelocity"的返回值,因为它不是一个变量"。

I apologise if this is a novel issue, I'm quite new to c# and unity.

如果这是一个新问题,我很抱歉,我对C#和Unity还不太熟悉。

The aforementioned script is below:

上述的脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;

public class EnemyGFX : MonoBehaviour
{
    public AIPath aiPath;

    // Update is called once per frame
    void Update()
    {
        if(aiPath.desiredVelocity.x >= 0.01f)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }  
        else if (aiPath.desiredVelocity.x == -0.01f)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        } 
    }
}
使用 System.Collections;
使用 System.Collections.Generic;
使用 UnityEngine;
使用 Pathfinding;

public class EnemyGFX : MonoBehaviour
{
    public AIPath aiPath;

    // 更新在每一帧调用
    void Update()
    {
        if(aiPath.desiredVelocity.x >= 0.01f)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }  
        else if (aiPath.desiredVelocity.x == -0.01f)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        } 
    }
}

希望这对您有帮助。如果您有任何其他问题,欢迎提出。

英文:

I have a script that should flip the sprite left or right depending on the direction of the path it is taking but when I save the script I get the following error, "Cannot modify the return value of "AIBase.desiredVelocity" because it is not a variable".

I apologise if this is a novel issue, I'm quite new to c# and unity.

The aforementioned script is below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;

public class EnemyGFX : MonoBehaviour
{
    public AIPath aiPath;

    // Update is called once per frame
    void Update()
    {
        if(aiPath.desiredVelocity.x >= 0.01f)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }  
        else if (aiPath.desiredVelocity.x = -0.01f)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        } 
    }
}




答案1

得分: 1

在你的 "else if" 行中,你使用了赋值运算符("="),你可能想使用小于或等于比较运算符("<=")。

赋值运算符会将左侧的值设置为右侧的值,就像你设置刻度一样。

比较运算符返回一个布尔值,if 语句可以对其进行评估。

这就是为什么错误消息说你试图修改 desiredVelocity 值。

英文:

In your "else if" line, you're using the assignment operator ("=") where you probably meant to use the less-than-or-equal-to comparison operator ("<=").

The assignment operator sets the value on the left to the value on the right, like you do to set the scales.

The comparison operator returns a boolean value that the if-statement can evaluate.

That's why the error message says that you're trying to modify the desiredVelocity value.

huangapple
  • 本文由 发表于 2023年2月14日 02:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定