Beispiel #1
0
    void Awake()
    {
        moveDirection = transform.TransformDirection(Vector3.forward);

        controller = this.GetComponent <CharacterController>();

        charAvatar = this.gameObject.GetComponent <EQBrowser.Avatar>();

        charAvatar.SetCurGroundHeight(this.gameObject.transform.position.y);

        //TODO: Animation
    }
    void Awake()
    {
        m_colliderControl = this.gameObject.GetComponent <CharacterColliderController>();
        m_avatar          = this.gameObject.GetComponent <EQBrowser.Avatar>();
        m_charControl     = this.gameObject.GetComponent <CharacterController>();

        m_avatar.SetCurGroundHeight(this.gameObject.transform.position.y);

        if (m_colliderControl == null)
        {
            Debug.LogError(string.Format("Cannot find Character Collider Controller for {0}", this.gameObject.name));
        }

        if (m_avatar == null)
        {
            Debug.LogError(string.Format("Cannot find Avatar for {0}", this.gameObject.name));
        }
    }
    Vector3 ApplyGravityAndJump()
    {
        Vector3 verticalDir = Vector3.zero;

        if (m_keyJump && !m_isJumping && m_jumpTimer < Time.time)
        {
            //Jump height is 35% of total height
            verticalDir.y += CalculateJumpVerticalSpeed(m_colliderControl.MaxHeight * 0.35f);
            m_isJumping    = true;
            m_keyJump      = false;
            m_avatar.Jump();
        }

        bool isGrounded = IsGrounded();

        if (isGrounded)
        {
            //Are we grounded and were we jumping? Then we must've just landed
            if (m_isJumping)
            {
                m_jumpTimer = Time.time + m_minTimeBetweenJumps;
            }

            m_avatar.SetCurGroundHeight(this.gameObject.transform.position.y);

            //We're on the ground, so we're not jumping
            m_isJumping = false;
        }
        else
        {
            m_keyJump      = false;
            verticalDir.y -= (m_gravity * Time.deltaTime);
        }

        return(verticalDir);
    }
Beispiel #4
0
    void UpdateSmoothedMovementDirection()
    {
        Transform cameraTransform = Camera.main.transform;
        bool      grounded        = IsGrounded();

        //Forward vector relative to the camera along the x-z plane
        Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);

        forward.y = 0;
        forward   = forward.normalized;

        //Right vector relative to the camera
        //Always orthogonal to the forward vector
        Vector3 right = new Vector3(forward.z, 0, -forward.x);

        float v = Input.GetAxisRaw("Vertical");
        float h = Input.GetAxisRaw("Horizontal");

        //Are we moving backwards or looking backwards?
        if (v < -0.2f)
        {
            movingBack = true;
        }
        else
        {
            movingBack = false;
        }

        bool wasMoving = isMoving;

        isMoving = Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1f;

        //Target direction relative to the camera
        Vector3 targetDirection = h * right + v * forward;

        if (Input.GetKey(KeyCode.Q))
        {
            controller.gameObject.transform.Rotate(Vector3.up, -45f * Time.deltaTime);
            charAvatar.CharTurning(false);
        }

        if (Input.GetKey(KeyCode.E))
        {
            controller.gameObject.transform.Rotate(Vector3.up, 45f * Time.deltaTime);
            charAvatar.CharTurning(true);
        }

        //Grounded controls
        if (grounded)
        {
            //Lock camera for short period when transitioning moving & standing still
            lockCameraTimer += Time.deltaTime;

            if (isMoving != wasMoving)
            {
                lockCameraTimer = 0.0f;
            }

            //We store speed and direction seperately,
            //so that when the character stands still we still have a valid forward direction
            //moveDirection is always normalized, and we only update it if there is user input.
            if (targetDirection != Vector3.zero)
            {
                //If we are really slow, just snap to the target direction
                if (moveSpeed < walkSpeed * 0.9f && grounded)
                {
                    moveDirection = targetDirection.normalized;
                }
                //Otherwise smoothly turn towards it
                else
                {
                    moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000f);

                    moveDirection = moveDirection.normalized;
                }

                if (grounded)
                {
                    charAvatar.SetCurGroundHeight(this.gameObject.transform.position.y);
                }
            }

            //Smooth the speed based on the current target direction
            float curSmooth = speedSmoothing * Time.deltaTime;

            //Choose target speed
            //We want to support analog input but make sure you can't walk faster diagonally than just forward or sideways
            float targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);

            //Pick speed modifier
            if (Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift))
            {
                targetSpeed *= runSpeed;
            }
            else
            {
                targetSpeed *= walkSpeed;
            }

            moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
        }
        //In air controls
        else
        {
            //Lock camera while in air
            if (jumping)
            {
                lockCameraTimer = 0.0f;
            }

            if (isMoving)
            {
                inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
            }
        }
    }
    void Awake()
    {
        moveDirection = transform.TransformDirection(Vector3.forward);

        controller = this.GetComponent<CharacterController>();

        charAvatar = this.gameObject.GetComponent<EQBrowser.Avatar>();

        charAvatar.SetCurGroundHeight(this.gameObject.transform.position.y);

        //TODO: Animation
    }
    void Awake()
    {
        m_colliderControl = this.gameObject.GetComponent<CharacterColliderController>();
        m_avatar = this.gameObject.GetComponent<EQBrowser.Avatar>();
        m_charControl = this.gameObject.GetComponent<CharacterController>();

        m_avatar.SetCurGroundHeight(this.gameObject.transform.position.y);

        if (m_colliderControl == null)
        {
            Debug.LogError(string.Format("Cannot find Character Collider Controller for {0}", this.gameObject.name));
        }

        if (m_avatar == null)
        {
            Debug.LogError(string.Format("Cannot find Avatar for {0}", this.gameObject.name));
        }
    }