0
\$\begingroup\$

How do I detect the landing of the player without getting interrupt by the onGround check ?

I got 2 scripts, one is the normal player script and the other is what I was trying. in the onGround Script it activates the collider when I jump which is positioned under the player and when I dont press the jump button anymore it will disable the Collider again which checks if I am landed or not. It is working but when im jumping and press quick S and you will hear the landing sound more then just once, but when I jump high and hold S for 2 seconds and then Pushdown to the ground the sound appears once. So my Idea is how it checks the landing correctly without the problem in the last sentence. And the Pushdown() Code is when I jump and press S while in the air it smashes down to the ground. To sum up I want to check the landing correctly.

Player Code:


public class PlayerScript : MonoBehaviour {

    //MOVEMENT
    public float moveSpeed;
    public float pushDownForce;
    //PUSHDOWN
    public GameObject pushDownParticle;
    bool pushdown;
    private Vector3 offset;
    public GameObject lander;
    private AudioSource audo;
    public AudioClip audoClip;
    public Transform checkGround;
    //JUMP
    public bool onGround;
    bool onLand;
    public float jumpPressure;
    private float minJump;
    private float maxJumpPressure;
    private Rigidbody rb;
    //ANIMATION
    private Animator anim;
    //PHYSICS
    public PhysicMaterial pm;


    void Start () 
    {
        //JUMP
        onGround = true;
        onLand = false;
        jumpPressure = 0f;
        minJump = 2f;
        maxJumpPressure = 10f;
        rb = GetComponent<Rigidbody>();
        //ANIMATION
        anim = GetComponent<Animator>();
        //PUSHDOWN
        audo = GetComponent<AudioSource>();
        //checkGround.gameObject.SetActive(false);
        checkGround.GetComponent<Collider>().enabled = false;
    }

    void FixedUpdate () 
    { 
        //ANIMATION

        transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed, 0f, 0f);

        //PushDownPhysics
        if(Input.GetKey(KeyCode.S))
        {
            if(!onGround)
            {
                //checkGround.gameObject.SetActive(true);
                checkGround.GetComponent<Collider>().enabled = true;
                pm.bounciness = 0.25f;
                PushDown();

                /*if(onGround)
                {
                    audo.pitch = (Random.Range(0.95f, 1f));
                    audo.PlayOneShot(audoClip, 0.7F);
                }
                */
            }
            else
            {
                pm.bounciness = 0.5f;;
                //checkGround.gameObject.SetActive(false);
                checkGround.GetComponent<Collider>().enabled = false;
            }

            if(Input.GetKeyUp(KeyCode.S))
                //checkGround.gameObject.SetActive(false);
                checkGround.GetComponent<Collider>().enabled = false;
        }

        //JUMP
        if(onGround)
        {
            //holding JumpButton
            if(Input.GetButton("Jump"))
            {
                if(jumpPressure < maxJumpPressure)
                {
                    jumpPressure += Time.deltaTime * 8f;
                }
                else
                {
                    jumpPressure = maxJumpPressure;
                }
                //ANIMATION
                anim.SetFloat("jumpPressure", jumpPressure + minJump);
            }
            //not holding JumpButton
            else
            {
                //Jump
                if(jumpPressure > 0f)
                {
                    jumpPressure = jumpPressure + minJump;
                    rb.velocity = new Vector3 (0f, jumpPressure, 0f);
                    jumpPressure = 0f;
                    onGround = false;
                    anim.SetFloat("jumpPressure", 0f);
                    anim.SetBool("onGround", onGround);
                }
            }
        }
    }
    //JUMP
    void OnCollisionEnter( Collision other)
    {
        if(other.gameObject.CompareTag("ground"))
        {
            onGround = true;
            //ANIMATION
            anim.SetBool("onGround", onGround);
        }
    }

    void PushDown()
    {
        //PUSHES DOWN THE BODY 
        rb.AddForce(-transform.up * pushDownForce);
    }

}

onGround script:


public class onGroundScript : MonoBehaviour {

    private AudioSource audo;
    public AudioClip audoClip;


    void Start ()
    {
        audo = GetComponent<AudioSource>();
    }

    void Update()
    {
        if(Input.GetButton("Jump"))
        {
            gameObject.GetComponent<Collider>().enabled = false;
        }
    }

    void OnTriggerEnter (Collider other) 
    {
        if(other.gameObject.tag == "ground")
        {
            audo.PlayOneShot(audoClip, 1.0F);
            Debug.Log ("Sound played");
            gameObject.GetComponent<Collider>().enabled = true;
        }
        else
        {
            gameObject.GetComponent<Collider>().enabled = false;
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

You can use a layer Mask and check against it.

[SerializeField] private LayerMask groundLayer; bool onground = false;

Set the ground you use to a ground layer and in fixed Update call the

void FixedUpdate()
{
    GroundCheck();
}

You can then auto set grounded bool with GroundCheck();

void GroundCheck()
{
   onground = Physics.CheckSphere(FeetCheckPosition,ACollisionRadius,groundLayer)
}
\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Josh Apr 26 '17 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.