Exemplo n.º 1
0
        private void FixedUpdate()
        {
            if ((!PhotonNetwork.IsConnected || photonView.IsMine))
            {
                // Handle XZ Movement

                Vector3 xzInputVector = new Vector3(inputVector.x, 0, inputVector.y);

                // Base Movement direction on input vector
                xzInputVector = Quaternion.Euler(0, ClientManager.GetCameraRotation().eulerAngles.y, 0) * xzInputVector;

                float inputMagnitude = inputVector.magnitude;

                // Only accelerate if there is input being read
                if (inputMagnitude > MIN_VALUE)
                {
                    // Accelerate in the direction of input.
                    Vector3 accelerationVector = xzInputVector * acceleration;

                    if (!isGrounded)
                    {
                        accelerationVector *= airControl;
                    }

                    if (xzVelocity.magnitude <= maxVelocity)
                    {
                        xzVelocity = Vector3.ClampMagnitude(
                            xzVelocity + accelerationVector,
                            maxVelocity
                            );
                    }
                    else
                    {
                        xzVelocity = xzVelocity + accelerationVector;
                    }
                }



                // Handle Y Movement

                if (isGrounded)
                {
                    if (jumpPressed)
                    {
                        yVelocity = jumpSpeed;

                        animator.SetTrigger("Jump");
                    }

                    if (yVelocity < 0)
                    {
                        yVelocity = 0;
                    }
                }
                else
                {
                    yVelocity -= gravity;
                }

                jumpPressed = false;



                // Check if Cube is Grounded

                RaycastHit groundHitInfo;

                bool isTouchingGround = Physics.SphereCast(
                    transform.position + collider.center,
                    collider.radius - MIN_VALUE,
                    Vector3.down,
                    out groundHitInfo,
                    MIN_VALUE * 2,
                    Global.TerrainMask
                    );

                isGrounded = (isTouchingGround && yVelocity <= 0);



                // Apply friction
                if (isGrounded)
                {
                    xzVelocity -= xzVelocity * frictionCoefficient;
                }
                else
                {
                    xzVelocity -= xzVelocity * frictionCoefficient * airControl;
                }

                if (xzVelocity.magnitude < MIN_VALUE)
                {
                    xzVelocity = Vector3.zero;
                }



                // Apply final movement

                rigidbody.velocity = new Vector3(xzVelocity.x, yVelocity, xzVelocity.z);



                // Set Animator Parameters

                animator.SetBool("Is Grounded", isGrounded);

                animator.SetFloat("Y Velocity", yVelocity);

                animator.SetFloat("Move Speed", inputMagnitude);



                // Check Death Zone

                if (transform.position.y < -50)
                {
                    Die();
                }
            }
        }
Exemplo n.º 2
0
 private void Update()
 {
     transform.rotation = ClientManager.GetCameraRotation();
 }