2017-04-08 57 views
0

所以我有2布尔的“FacingRight”和“FacingLeft”他们不工作,因为我想和我想到一个更好的主意,但完全不知道如何去做。如何让BOOL成为真或假,基于浮动增加或减少

如果游戏对象(玩家)的X位置值增加,我想使FacingRight = true和FacingLeft = false,这意味着当X值减少时,它向右移动,FacingRight = false和FacingLeft = true。

任何帮助将不胜感激。

这是我目前的代码,如果有帮助。

public float fireAnimDelay; 
Animator anim; 

public bool FacingRight = false; 
public bool FacingLeft = false; 

// Use this for initialization 
void Awake() 
{ 
    anim = GetComponent<Animator>(); 
    anim.GetFloat("Speed"); 
} 

// Update is called once per frame 
void Update() 
{ 
    if (anim.GetFloat("Speed") >= 0.1f) // If speed is positive you are facing right 
    { 
     FacingRight = true; 
     Debug.Log("FacingRight"); 
    } 

    if (anim.GetFloat("Speed") <= -0.1f) // if speed is negative you are facing left 
    { 
     FacingLeft = true; 
    } 

    if (anim.GetFloat("Speed") == 0.0f) // if speed is 0 you are idle 
    { 
     FacingRight = false; 
     FacingLeft = false; 
    } 

    if (Input.GetKey(KeyCode.Z) && FacingRight == true) 
    { 
     FacingLeft = false; 

     anim.SetBool("Firing", true); 
     anim.SetBool("FacingRight", true); 
     anim.SetBool("FacingLeft", false); 
     StartCoroutine(FireDelay()); 
    } 

    if (Input.GetKey(KeyCode.Z) && FacingLeft == true) 
    { 
     FacingRight = true; 

     anim.SetBool("Firing", true); 
     anim.SetBool("FacingLeft", true); 
     anim.SetBool("FacingRight", false); 
     StartCoroutine(FireDelay()); 
    } 
} 

IEnumerator FireDelay() 
{ 
    yield return new WaitForSeconds(fireAnimDelay); 
    anim.SetBool("Firing", false); 
} 

回答

0

实现此目的的有效方法是利用一个或多个事件。尽管您也可以使用其他技术来实现此目的,但基于事件的编程为您提供了无价的优势,其中最简单的优点是,当您的值在不同的上下文中发生更改时,您可以运行不同的任务。 (你可以通过谷歌阅读更多的事件)

下面的代码提供了如何应用在代码中类似的例子:

public class FloatIncreasing 
{ 
    private float _Value; 
    public float Value { 
     get { 
      return _Value; 
     } 
     set { 
      ValueChangeEvent(this, _Value >= value); 
      _Value = value; 
     } 
    } 
    public Boolean HasIncreased { get; private set; } 
    public event EventHandler<Boolean> ValueChangeEvent; 
    public FloatIncreasing() { 
     this.ValueChangeEvent += OnValueChanged; 
    } 
    public virtual void OnValueChanged(Object sender, Boolean value) { 
     HasIncreased = value; 
     // You can add a more appropriate default behavior in here. 
    } 
} 

考虑到这一点的代码,你可以使用它的方式如下:

class Program 
{ 
    static FloatIncreasing test = new FloatIncreasing(); 
    static void Main(string[] args) { 
     test.ValueChangeEvent += Test_ValueChangeEvent; 
     test.Value = 0; 
     test.Value = 10; 
     test.Value = 3; 
     Console.ReadKey(true); 
    } 

    private static void Test_ValueChangeEvent(object sender, bool e) 
    { 
     Console.WriteLine(e ? "Value Decreased or Not Changed" : "Value increased"); 
    } 
} 

请注意,任何时候,test.Value改变时,就会触发相应的事件,允许您修改布尔值。

+0

我对C#和Unity非常陌生,所以在这里有很多东西我没有得到,如果有人可以进一步打破这个对我来说这将是可怕的。 – OmBiEaTeR

+0

我也会在此期间进行研究,并希望能够这样工作 – OmBiEaTeR

+0

@OmBiEaTeR用最简单的话来说,你需要的是一个事件。 '当C#中的一个事件是当一个对象发生一些有趣的事情(变化)时,一个类给这个类的客户端提供通知的方法' – Transcendent

0

你有一个逻辑错误在你的代码:

if (Input.GetKey(KeyCode.Z) && FacingRight == true) { 
    FacingLeft = false; 
} 

if (Input.GetKey(KeyCode.Z) && FacingLeft == true) { 
    FacingRight = true; 
} 

注意如何在一个实例中,如果向右设置左为假,但在免费块你右键设置为true!

此外,anim.GetFloat("Speed") >= 0.1f您应该检查比0 <>,否则为0和0.1之间的值都不会被检测(或者使用IF ... ELSEIF ... ELSE)。

一种更好的方法是将FacingLeft /右变量降低到单个可变:

enum Facing { NONE, LEFT, RIGHT } 
Facing playerFacing = Facing.NONE; 

//... 

if (Input.GetKey(KeyCode.Z) && playerFacing == Facing.RIGHT) { 
    //... 
} 

除了使用Input.GetButton("Fire")而不是硬编码键码。这将允许你的球员重新绑定键(也许他们不喜欢你的WASD运动设置,并且更喜欢HJKL)。