Example #1
0
        public void OnCollisionEnter(Collision collision)
        {
            SimpleDamage damage = collision.collider.GetComponent <SimpleDamage>();

            if (damage != null)
            {
                damage.TakeDamage(0.6f, owner);
            }
        }
        private void FixedUpdate()
        {
            if (Input.GetKey(KeyCode.K) == true)
            {
                damage.TakeDamage(100);
            }

            // Check for dead
            if (damage.IsDead == true)
            {
                return;
            }

            // Update animator
            anim.SetBool("PlayerInSight", true);
            anim.SetFloat("Speed", controller.velocity.magnitude);

            // Get the input
            float x = Input.GetAxis("Horizontal");
            float y = Input.GetAxis("Vertical");

            // Limit diagonal speed
            float modify = (x != 0 && y != 0) ? 0.707f : 1;

            // Check for grounded
            if (isGrounded == true)
            {
                bool isSliding = false;

                RaycastHit hit;

                // Check for sliding surface
                if (Physics.Raycast(transform.position, Vector3.down, out hit, rayDistance) == true)
                {
                    // Check if we can slide
                    if (Vector3.Angle(hit.normal, Vector3.up) > controller.slopeLimit - 0.1f)
                    {
                        // Set the sliding flag
                        isSliding = true;
                    }
                }
                else
                {
                    // Raycast at contact point to catch steep inclines
                    Physics.Raycast(contactPoint + Vector3.up, Vector3.down, out hit);

                    // Check if we can slide
                    if (Vector3.Angle(hit.normal, Vector3.up) > controller.slopeLimit - 0.1f)
                    {
                        // Set the sliding flag
                        isSliding = true;
                    }
                }

                // Check if we are currently falling
                if (isFalling == true)
                {
                    // Disbale the flag since we are grounded
                    isFalling = false;
                }

                // Check if we are sliding
                if (isSliding == true)
                {
                    // Get the slide normal
                    Vector3 normal = hit.normal;

                    // Update the move direction
                    moveDirection = new Vector3(normal.x, -normal.y, normal.z);

                    // Normalize the direction
                    Vector3.OrthoNormalize(ref normal, ref moveDirection);

                    // Apply the speed
                    moveDirection *= walkSpeed;
                }
                else
                {
                    // Apply movement based on input
                    moveDirection = new Vector3(x * modify, -0.75f, y * modify);
                    moveDirection = transform.TransformDirection(moveDirection) * walkSpeed;
                }

                if (Input.GetButton("Jump") == true)
                {
                    // Incrmement the jump counter
                    jumpTimer++;
                }
                else if (jumpTimer >= 1)
                {
                    // Trigger a jump
                    Jump(jumpHeight);
                }
            }
            else
            {
                // Check if we are not falling
                if (isFalling == false)
                {
                    // We should be falling
                    isFalling = true;
                }
            }

            // Apply gravity
            moveDirection.y -= gravity * Time.deltaTime;

            // Apply the movement to the controller
            CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);

            // Check for grounded
            isGrounded = (flags & CollisionFlags.Below) != 0;
        }