簡體   English   中英

Unity中的嚴重問題

[英]Crouching issues in Unity

因此,我在Unity 2D游戲中創建了蹲伏,除了在退出蹲伏后角色上方有某物時,一切都進行得很好,它的作用是返回到空閑動畫並減小了Y軸位置,我知道這是正常現象,但是如何當角色上方有東西或按住ctrl按鈕時,我可以蹲着嗎? 如有必要,請在下面檢查腳本。 謝謝!

    private Rigidbody2D rb2d;
    private float h = 0.0f;
    public float Speed, Jump;
    private bool canJump;
    private Animator anim;
    private BoxCollider2D bc2d;

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        bc2d = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {

        h = Input.GetAxisRaw("Horizontal");

        anim.SetFloat("Speed", Mathf.Abs(h));
        transform.Translate(Vector3.right * h * Speed * Time.deltaTime);
        if (h != 0.0f)
        {
            transform.localScale = new Vector2(h, transform.localScale.y);
        }
        if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
        {
            rb2d.AddForce(new Vector2(rb2d.velocity.x, Jump));
        }

        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            bc2d.enabled = false;
            Speed = Speed / 2;
            anim.SetBool("Crouch", true);
        }
        else if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            bc2d.enabled = true;
            Speed = Speed * 2;
            anim.SetBool("Crouch", false);
        }









    }




    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.name == "Ground")
        {
            canJump = true;
            anim.SetBool("Jump", false);
        }

    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.name == "Ground")
        {
            canJump = false;
            anim.SetBool("Jump", true);
        }

    }

使用RaycastHit2D 基本上,在“站立”之前,從角色投射光線Vector2.up,如果命中,請檢查並確保有足夠的空間站立。 就像是:

private Rigidbody2D rb2d;   
private Transform t;
private float h = 0.0f;
public float Speed, Jump;
private bool canJump;
private Animator anim;
private BoxCollider2D bc2d;

// Variable to check if character is attempting to stand
private bool tryingToStand = false;
// Keep crouching state
private bool isCrouching = false;

// Start is called before the first frame update
void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    bc2d = GetComponent<BoxCollider2D>();
    t = GetComponent<Transform>();
}

// Update is called once per frame
void Update()
{
    h = Input.GetAxisRaw("Horizontal");

    anim.SetFloat("Speed", Mathf.Abs(h));
    transform.Translate(Vector3.right * h * Speed * Time.deltaTime);
    if (h != 0.0f)
    {
        transform.localScale = new Vector2(h, transform.localScale.y);
    }
    // Make sure we can jump and we're not crouching before jumping
    if (Input.GetKeyDown(KeyCode.Space) && canJump == true && !isCrouching)
    {
        rb2d.AddForce(new Vector2(rb2d.velocity.x, Jump));
    }

    if (Input.GetKeyDown(KeyCode.LeftControl))
    {
        bc2d.enabled = false;
        anim.SetBool("Crouch", true);
        // Make sure we haven't already halved the speed
        if (!tryingToStand)
        {
            Speed = Speed / 2;
        }
        // Set tryingToStand to false in case player holds ctrl before character has stood up
        tryingToStand = false;
        // Set crouching state to true
        isCrouching = true;
    }
    else if (Input.GetKeyUp(KeyCode.LeftControl))
    {
        tryingToStand = true;            
    }

    if (tryingToStand && CanStand())
    {
        tryingToStand = false;
        // Set crouching state to false;
        isCrouching = false;
        bc2d.enabled = true;
        Speed = Speed * 2;
        anim.SetBool("Crouch", false);
    }
}

bool CanStand()
{        
    RaycastHit2D hit = Physics2D.Raycast(t.position, Vector2.up);
    if (hit.collider != null)
    {
        // Check the distance to make sure the character has clearance, you'll have to change the 1.0f to what makes sense in your situation.
        if (hit.distance <= 1.0f)
        {
            return false;
        }
    }
    return true;    
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.name == "Ground")
    {
        canJump = true;
        anim.SetBool("Jump", false);
    }    
}

private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.collider.name == "Ground")
    {
        canJump = false;
        anim.SetBool("Jump", true);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM