英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论