// Update is called once per frame void Update() { if (rippleSurface != null && rippleSurface.isPositionOnRippleSurface(transform.position)) { // Sample the height of the ripple surface float height = rippleSurface.sampleMeshHeight(transform.position); // Set our y position to the ripple surface height Vector3 pos = transform.position; pos.y = height + yOffset; transform.position = pos; } }
void CheckGroundStatus() { RaycastHit hitInfo; #if UNITY_EDITOR // helper to visualise the ground check ray in the scene view Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance)); #endif // 0.1f is a small offset to start the ray from inside the character // it is also good to note that the transform position in the sample assets is at the base of the character if (m_IsGrounded && currentRippleSurface != null) { if (currentRippleSurface.isPositionOnRippleSurface(transform.position)) { // Sample the height of the ripple surface float height = currentRippleSurface.sampleMeshHeight(transform.position); // Set our y position to the ripple surface height Vector3 pos = transform.position; pos.y = height + yOffset; transform.position = pos; m_rb.useGravity = false; // if height is greater than knockup threshold, knock the character into the air if (height - currentRippleSurface.transform.position.y > knockupThreshold) { m_rb.velocity = new Vector3(m_rb.velocity.x, knockupPower, m_rb.velocity.z); m_IsGrounded = false; //m_Animator.applyRootMotion = false; m_GroundCheckDistance = 0.1f; m_rb.useGravity = true; } } else { // We are no longer in the bounds of the ripple surface, set us to not be grounded so we can check if there is anything under us. currentRippleSurface = null; m_IsGrounded = false; m_GroundNormal = Vector3.up; //m_Animator.applyRootMotion = false; m_rb.useGravity = true; } } else if (!m_IsGrounded) { if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo)) { RippleDeformer ripple = hitInfo.collider.GetComponent <RippleDeformer>(); if (ripple) // We are above a surface that can ripple { currentRippleSurface = ripple; // Sample the height of the ripple surface float height = currentRippleSurface.sampleMeshHeight(hitInfo); float distanceToGround = transform.position.y - yOffset - height; if (distanceToGround > dropDistanceToDie) { willDie = true; } // Check if we can land (we are close to the ground and our velocity is downwards if (distanceToGround <= m_GroundCheckDistance && m_rb.velocity.y <= 0) { // We've hit the ripple surface, die m_GroundNormal = hitInfo.normal; m_IsGrounded = true; ////m_Animator.applyRootMotion = true; m_rb.useGravity = false; if (willDie) { onDeath(); } } } else // We are above a surface that cannot ripple { if (Vector3.Distance(hitInfo.point, transform.position) < m_GroundCheckDistance) { m_GroundNormal = hitInfo.normal; m_IsGrounded = true; //m_Animator.applyRootMotion = true; } else { m_IsGrounded = false; m_GroundNormal = Vector3.up; //m_Animator.applyRootMotion = false; } } } } }
void CheckGroundStatus() { RaycastHit hitInfo; #if UNITY_EDITOR // helper to visualise the ground check ray in the scene view Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance)); #endif // 0.1f is a small offset to start the ray from inside the character // it is also good to note that the transform position in the sample assets is at the base of the character if (m_IsGrounded && currentRippleSurface != null) { if (currentRippleSurface.isPositionOnRippleSurface(transform.position)) { // Sample the height of the ripple surface float height = currentRippleSurface.sampleMeshHeight(transform.position); // Set our y position to the ripple surface height Vector3 pos = transform.position; pos.y = height; transform.position = pos; m_Rigidbody.useGravity = false; // if height is greater than knockup threshold, knock the character into the air if (!immuneToKnockup && height - currentRippleSurface.transform.position.y > knockupThreshold) { m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z); m_IsGrounded = false; m_Animator.applyRootMotion = false; m_GroundCheckDistance = 0.1f; m_Rigidbody.useGravity = true; } } else { // We are no longer in the bounds of the ripple surface, set us to not be grounded so we can check if there is anything under us. currentRippleSurface = null; m_IsGrounded = false; m_GroundNormal = Vector3.up; m_Animator.applyRootMotion = false; m_Rigidbody.useGravity = true; } } else if (!m_IsGrounded) { if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo)) { RippleDeformer ripple = hitInfo.collider.GetComponent <RippleDeformer>(); if (ripple) // We are above a surface that can ripple { currentRippleSurface = ripple; // Sample the height of the ripple surface float height = currentRippleSurface.sampleMeshHeight(hitInfo); // Check if we can land (we are close to the ground and our velocity is downwards if (transform.position.y - height <= m_GroundCheckDistance && m_Rigidbody.velocity.y <= 0) { // We've hit the ripple surface, grant us knockup immunity immuneToKnockup = true; immunityFromKnockupTimer = IMMUNITY_DURATION; // Scale the mesh hit force based on the downward velocity float velocity = -m_Rigidbody.velocity.y; currentRippleSurface.splashDiameter = (int)Mathf.Lerp(5, 15, velocity / 20); currentRippleSurface.hitMesh(hitInfo); m_GroundNormal = hitInfo.normal; m_IsGrounded = true; m_Animator.applyRootMotion = true; m_Rigidbody.useGravity = false; // Instantiate ground smash particles GameObject g = Instantiate <GameObject>(hitGroundParticles); g.transform.position = transform.position; fall.clip = falling; fall.Play(); } } else // We are above a surface that cannot ripple { //if (Vector3.Distance(hitInfo.point, transform.position) < m_GroundCheckDistance) //{ // m_GroundNormal = hitInfo.normal; // m_IsGrounded = true; // m_Animator.applyRootMotion = true; //} //else //{ // m_IsGrounded = false; // m_GroundNormal = Vector3.up; // m_Animator.applyRootMotion = false; //} } } } }
void CheckGroundStatus() { RaycastHit hitInfo; #if UNITY_EDITOR // helper to visualise the ground check ray in the scene view Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance)); #endif // 0.1f is a small offset to start the ray from inside the character // it is also good to note that the transform position in the sample assets is at the base of the character if (m_IsGrounded && currentRippleSurface != null) { if (currentRippleSurface.isPositionOnRippleSurface(transform.position)) { // Sample the height of the ripple surface float height = currentRippleSurface.sampleMeshHeight(transform.position); // Set our y position to the ripple surface height Vector3 pos = transform.position; pos.y = height; transform.position = pos; m_Rigidbody.useGravity = false; } else { // We are no longer in the bounds of the ripple surface, set us to not be grounded so we can check if there is anything under us. currentRippleSurface = null; m_IsGrounded = false; m_GroundNormal = Vector3.up; m_Animator.applyRootMotion = false; m_Rigidbody.useGravity = true; } } if (!m_IsGrounded) { if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo)) { RippleDeformer ripple = hitInfo.collider.GetComponent <RippleDeformer>(); if (ripple) // We are above a surface that can ripple { currentRippleSurface = ripple; // Sample the height of the ripple surface float height = currentRippleSurface.sampleMeshHeight(hitInfo); // Check if we can land if (transform.position.y - height <= m_GroundCheckDistance) { // We've hit the ripple surface // Scale the mesh hit force based on the downward velocity float velocity = -m_Rigidbody.velocity.y; currentRippleSurface.splashDiameter = (int)Mathf.Lerp(5, 25, velocity / 20); currentRippleSurface.hitMesh(hitInfo); m_GroundNormal = hitInfo.normal; m_IsGrounded = true; m_Animator.applyRootMotion = true; m_Rigidbody.useGravity = false; } } else // We are above a surface that cannot ripple { if (Vector3.Distance(hitInfo.point, transform.position) < m_GroundCheckDistance) { m_GroundNormal = hitInfo.normal; m_IsGrounded = true; m_Animator.applyRootMotion = true; } else { m_IsGrounded = false; m_GroundNormal = Vector3.up; m_Animator.applyRootMotion = false; } } } } }