简体   繁体   English

Unity中的严重问题

[英]Crouching issues in Unity

So I've created crouching in Unity 2D game, everything goes well except when there is something above the character after exiting crouching, what it does is that it returns to idle animation and decreases Y-axis position, I know that is normal but how can I keep the character crouched when there is something above him or when I hold the ctrl button? 因此,我在Unity 2D游戏中创建了蹲伏,除了在退出蹲伏后角色上方有某物时,一切都进行得很好,它的作用是返回到空闲动画并减小了Y轴位置,我知道这是正常现象,但是如何当角色上方有东西或按住ctrl按钮时,我可以蹲着吗? Check the script down below if necessary. 如有必要,请在下面检查脚本。 Thanks! 谢谢!

    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);
        }

    }

Use RaycastHit2D . 使用RaycastHit2D Basically before "standing" cast a ray Vector2.up from the character and if it hits, check to make sure there's enough room to stand. 基本上,在“站立”之前,从角色投射光线Vector2.up,如果命中,请检查并确保有足够的空间站立。 Something like: 就像是:

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