2017-06-16 124 views
0

Sup?我有一个空闲状态,用于我的玩家角色,这是默认状态。但是,我想要一个二级闲置。当我按下C键时,它会进入第二个空闲动画并停留在那里。当我按下x键时,它会回到默认的空闲动画。但这是问题开始的地方。当我按下C键再次切换到辅助动画时,会很快跳到第二个空闲动画,但会返回到默认动画,而无需保留或等待任何其他命令。我希望它留在我告诉它的地方。在两个空闲动画状态之间切换

此外,问题发生后,我再次点击C键,动画不会改变。但是,当我点击X键而不是此时,然后是C键后,它又一次在动画之间来回跳动。所以我认为,它'认为'它已经被切换,当它没有。如果你能告诉我该如何解决这个问题,我将成为你最好的朋友。谢谢。

using UnityEngine; 
using System.Collections; 


public class Player : MonoBehaviour 
{ 

private Animator anim; 
Rigidbody2D rb; 
public float Speed; 
private bool aim = false; 
private bool shot = false; 
private bool idle = true; 
public Transform arrowSpawn; 
public GameObject arrowPrefab; 
private bool idle2 = false; 


void Start() 
{ 

    rb = GetComponent<Rigidbody2D>(); 
    anim = GetComponent<Animator>(); 

} 

// Update is called once per frame 
void Update() 
{ 
    Movement(); 
    Inputer(); 
    Morph(); 

} 

void Movement() 
{ 
    float moveH = Input.GetAxis("Horizontal"); 

    { 
     rb.velocity = new Vector2 (moveH * Speed, rb.velocity.y); 
    } 

    anim.SetFloat ("Speed", Mathf.Abs (moveH)); 


} 

void Inputer() 
{ 

    if (!aim && Input.GetKeyDown (KeyCode.S)) { 

     aim = true; 
     anim.SetTrigger ("AIm"); 
    } 

    if (aim && Input.GetKeyUp (KeyCode.S)) { 
     shot = true; 
     anim.SetTrigger ("Shot"); 

    } 

    if (shot) { 
     shot = false; 
     aim = false; 
     idle = true; 
     anim.SetTrigger ("Idle"); 
     Instantiate (arrowPrefab, arrowSpawn.position, arrowSpawn.rotation); 

    } 
} 

    void Morph() 
{ 
    idle = !idle2; 

    if (idle && Input.GetKeyDown (KeyCode.C)) { 
     idle2 = true; 
     anim.SetTrigger ("idle2"); 
    } 

    if (!idle && Input.GetKeyDown (KeyCode.X)) { 

     idle = true; 
     anim.SetTrigger ("Idle"); 
     idle2 = false; 

    } 
}   

}

回答

0

我不知道 - 当我通过逻辑跟踪,似乎所有的工作,但是......好吧,闲置和空闲2处理似乎有点可疑。

我的意思是,看看逻辑上的 'X' 键:

  1. 打开空闲的
  2. 揭开序幕闲置触发
  3. 打开IDLE2关闭

...并在'C'键上:

  1. 打开idle2 on
  2. 启动idle2触发器

...哪里有闲置关闭?似乎要等待下一个Morph()函数设置该变量。更糟糕的是,如果idle2在代码中的其他地方被设置为false,那么你的Morph()函数会将空闲转换为真实......但是不会启动你的空闲触发器。

我建议尝试这些了一个,看看他们为你工作:

void Morph() 
{ 
    if (!idle && !idle2) 
     anim.SetTrigger ("Idle"); 
    idle = !idle2; 

    if (idle && Input.GetKeyDown (KeyCode.C)) { 
     idle2 = true; 
     anim.SetTrigger ("idle2"); 
    } 

    if (!idle && Input.GetKeyDown (KeyCode.X)) { 
     idle = true; 
     anim.SetTrigger ("Idle"); 
     idle2 = false; 
    } 
} 

......或者......

void Morph() 
{ 
    if (idle && Input.GetKeyDown (KeyCode.C)) { 
     idle2 = true; 
     anim.SetTrigger ("idle2"); 
     idle = false; 
     return; 
    } 
    if (!idle && Input.GetKeyDown (KeyCode.X)) { 
     idle = true; 
     anim.SetTrigger ("Idle"); 
     idle2 = false; 
     return; 
    } 
} 
+0

我只是想他们。结果完全一样。我知道某个地方的问题与闲置的问题有关,并且您确实清理了一些代码。我没有考虑投入回报,所以非常感谢。但问题仍然存在于两个版本中。 –

+0

哦!只是想出了最好的朋友!我在称为idle1的动画制作者中制作了一个辅助触发器。然后我把它从第二个闲置链接到第一个使用idle1,并用anim代替它。用anim替换它。用SetStrigger命令(“Idle1”);感谢您的协助! –