C# 当一个敌人的精灵在团结中转身时,他会翻转它

C# 当一个敌人的精灵在团结中转身时,他会翻转它,c#,unity3d,2d,sprite,flip,C#,Unity3d,2d,Sprite,Flip,我已经花了几个小时试图解决这个问题,我尝试了很多方法,但都没有成功 所以我有一个使用AI跟随玩家的敌人,当敌人左转或右转时,我需要精灵翻转 这是我的一部分代码(很多代码都是关于人工智能的,所以我只发布其中的一部分) Vector3 dir=(path.vectorPath[currentWaypoint]-transform.position).标准化; dir*=速度*时间。固定时间; //移动人工智能 rb.AddForce(dir,fMode); 无效更新(){ 如果(rb.AddForc

我已经花了几个小时试图解决这个问题,我尝试了很多方法,但都没有成功

所以我有一个使用AI跟随玩家的敌人,当敌人左转或右转时,我需要精灵翻转

这是我的一部分代码(很多代码都是关于人工智能的,所以我只发布其中的一部分)

Vector3 dir=(path.vectorPath[currentWaypoint]-transform.position).标准化;
dir*=速度*时间。固定时间;
//移动人工智能
rb.AddForce(dir,fMode);
无效更新(){
如果(rb.AddForce>0){
sr.flipX=false;
}否则如果(rb.AddForce<0)
sr.flipX=true;
animate.SetFloat(“pMove”,Mathf.Abs(rb.AddForce));
}

假设,
sr
是一个
spriteender
组件,
sr.flipX
方法很好。但是,您对
刚体上的力的评估不正确
rb.AddForce
的返回类型为
void
,读取
刚体
是否受力的正确方法是读取
rb.velocity.magnity
。另外,
rb.velocity
将以
Vector3
的形式为您提供游戏对象的速度方向。假设您正在X轴上工作,将其放入您的
LateUpdate
方法中:

sr.flipX = rb.velocity.magnitude > 0 && rb.velocity.x < 0 ? true : false;
sr.flipX=rb.velocity.magnity>0&&rb.velocity.x<0?真:假;
如果
刚体正在移动(
rb.velocity.magnity>0
),并且它正在向左侧移动(
rb.velocity.X<0
),则更新
方法将沿X轴翻转精灵,而不是使用if-else块


在您的问题中,您只要求翻转精灵:因为,
flipX
只影响渲染,而不影响其他组件(如碰撞器和动画)

翻转精灵最简单的方法是使用
localScale.x*=-1
而不是检查AddForce,您应该检查刚体在x轴上的速度(如果翻转,则检查y轴,根据精灵是跳跃还是下落,检查精灵的速度)

基本上,在
Update()
中,您可以这样做:
vx=rigidbody.velocity.x以存储精灵x轴上的速度。然后在
LastUpdate()
中检查是否需要翻转精灵:

if (vx > 0) {
    facingRight = true;
} else if (vx < 0) { 
    facingRight = false;
}

if (((facingRight) && (localScale.x<0)) || ((!facingRight) && (localScale.x>0))) {
    localScale.x *= -1;
}
if(vx>0){
facingRight=正确;
}如果(vx<0){
facingRight=错误;
}
如果(((朝向右侧)和&(localScale.x0))){
localScale.x*=-1;
}
这里有一个完整的例子,精灵根据玩家的输入移动。你需要为你的AI添加它

//store references to components on the gameObject
Transform transform;
Rigidbody2D rigidbody;

public float MoveSpeed = 3f;

// hold player motion in this timestep
float vx;
float vy;

Awake () {
    // get a reference to the components we are going to be changing and store a reference for efficiency purposes
    transform = GetComponent<Transform> ();
    rigidbody = GetComponent<Rigidbody2D> ();

}


void Update()
{
    // determine horizontal velocity change based on the horizontal input
    vx = Input.GetAxisRaw ("Horizontal");

    //Change in case you are jumping or falling
    vy = rigidbody.velocity.y;

    // Change the actual velocity on the rigidbody
    rigidbody.velocity = new Vector2(_vx * MoveSpeed, _vy);

}

// Checking to see if the sprite should be flipped
// this is done in LateUpdate since the Animator may override the localScale
// this code will flip the player even if the animator is controlling scale
void LateUpdate()
{
    // get the current scale
    Vector3 localScale = transform.localScale;

    if (vx > 0) // moving right so face right
    {
        facingRight = true;
    } else if (vx < 0) { // moving left so face left
        facingRight = false;
    }

    // check to see if scale x is right for the player
    // if not, multiple by -1 which is an easy way to flip a sprite
    if (((facingRight) && (localScale.x<0)) || ((!facingRight) && (localScale.x>0))) {
        localScale.x *= -1;
    }   

    // update the scale
    transform.localScale = localScale;
}
//存储对游戏对象上组件的引用
变换;
刚体2D刚体;
公共浮动速度=3f;
//在此时间步中保持播放器运动
浮动vx;
浮球;
醒着(){
//获取我们将要更改的组件的引用,并存储引用以提高效率
transform=GetComponent();
刚体=GetComponent();
}
无效更新()
{
//根据水平输入确定水平速度变化
vx=Input.GetAxisRaw(“水平”);
//如果你在跳跃或跌倒,请更换
vy=刚体速度y;
//更改刚体上的实际速度
刚体速度=新矢量2(_vx*移动速度,_vy);
}
//检查精灵是否应该翻转
//这是在LateUpdate中完成的,因为动画师可能会覆盖localScale
//即使动画师正在控制缩放,此代码也将翻转播放器
void LateUpdate()
{
//获取当前刻度
Vector3 localScale=transform.localScale;
如果(vx>0)//向右移动,则面向右侧
{
facingRight=正确;
}如果(vx<0){//向左移动,则面向左
facingRight=错误;
}
//检查比例x是否适合玩家
//如果不是,则乘以-1,这是翻转精灵的简单方法
如果(((朝向右侧)和&(localScale.x0))){
localScale.x*=-1;
}   
//更新量表
transform.localScale=localScale;
}

rb.AddForce>0
??首先,
AddForce
是一个函数,也是一个
void
返回类型。我真的鼓励你在继续之前理解。为所有方向预先渲染精灵(除非你的游戏支持超过8个方向)比使用一些更有效trickery@Programmer我知道,但正如我所说,我尝试了很多方法,这是最后一种方法,我至少在检查,谁知道呢。这是一种巧妙的方法,就像将Y轴旋转设置为180度一样。然而,我想知道,在这个特殊的例子中,修改
刚体
变换
是否会迫使物理学重新计算
游戏对象
的碰撞器。当比例为-1时,X方向上的力会发生什么变化?我之所以这样问,是因为
刚体
的Unity文档明确强调了这一点,因为
刚体
上有扭矩和力。我稍后将阅读有关此链接的内容,我会让您知道。但到目前为止,我对这种方法没有任何问题。谢谢!我现在就试试这个,我正在睡觉:是的,它起作用了!非常感谢。现在我要学习更多的知识来帮助其他人。如果你想让你的雪碧批量在一起,那可不是个好主意。。负刻度会中断批处理。不建议您使用Spriterender flipX,因为它是专门为此而设计的。
//store references to components on the gameObject
Transform transform;
Rigidbody2D rigidbody;

public float MoveSpeed = 3f;

// hold player motion in this timestep
float vx;
float vy;

Awake () {
    // get a reference to the components we are going to be changing and store a reference for efficiency purposes
    transform = GetComponent<Transform> ();
    rigidbody = GetComponent<Rigidbody2D> ();

}


void Update()
{
    // determine horizontal velocity change based on the horizontal input
    vx = Input.GetAxisRaw ("Horizontal");

    //Change in case you are jumping or falling
    vy = rigidbody.velocity.y;

    // Change the actual velocity on the rigidbody
    rigidbody.velocity = new Vector2(_vx * MoveSpeed, _vy);

}

// Checking to see if the sprite should be flipped
// this is done in LateUpdate since the Animator may override the localScale
// this code will flip the player even if the animator is controlling scale
void LateUpdate()
{
    // get the current scale
    Vector3 localScale = transform.localScale;

    if (vx > 0) // moving right so face right
    {
        facingRight = true;
    } else if (vx < 0) { // moving left so face left
        facingRight = false;
    }

    // check to see if scale x is right for the player
    // if not, multiple by -1 which is an easy way to flip a sprite
    if (((facingRight) && (localScale.x<0)) || ((!facingRight) && (localScale.x>0))) {
        localScale.x *= -1;
    }   

    // update the scale
    transform.localScale = localScale;
}