示例#1
0
    // Update is called once per frame
    void Update()
    {
        if (PauseMenu.IsOn)
        {
            return;
        }

        float xMov = Input.GetAxis("Horizontal");
        float zMov = Input.GetAxis("Vertical");

        Vector3 movHorizontal = transform.right * xMov;
        Vector3 movVertical   = transform.forward * zMov;

        //final movement vector
        Vector3 velocity = (movHorizontal + movVertical) * speedPlayer;

        //apply movement
        motor.Move(velocity);

        //Rotation HORIZONTAL, we will turn the player to look around him.
        float   yRot           = Input.GetAxisRaw("Mouse X");
        Vector3 rotationPlayer = new Vector3(0f, yRot, 0f) * lookSensivility;

        //Apply
        motor.Rotate(rotationPlayer);

        //Rotation VERTICAL, we will turn the camera in a vertical axis, why? We dont wanna turn the player vertically only camera.
        float xRot           = Input.GetAxisRaw("Mouse Y");
        float cameraRotation = xRot * lookSensivility;

        //Apply
        motor.RotateCamera(cameraRotation);


        //Calculate Jump
        Vector3 jump = Vector3.zero;

        //Apply jump Force
        if (Input.GetButton("Jump"))
        {
            jump = Vector3.up * jumpForce;
            SetJointSettings(0f);
        }
        else
        {
            SetJointSettings(jointSpring);
        }

        motor.ApplyJump(jump);
    }
    // Update is called once per frame
    void Update()
    {
        float xMov = Input.GetAxis("Horizontal");
        float zMov = Input.GetAxis("Vertical");

        Vector3 movHorizontal = transform.right * xMov;
        Vector3 movVertical   = transform.forward * zMov;

        //final movement vector
        Vector3 velocity = (movHorizontal + movVertical) * speed / rb.mass;

        //apply movement
        motor.Move(velocity);
        float   yRot           = Input.GetAxisRaw("Mouse X");
        Vector3 rotationPlayer = new Vector3(0f, yRot, 0f) * lookSensivility;

        //Apply
        motor.Rotate(rotationPlayer);

        //Rotation VERTICAL, we will turn the camera in a vertical axis, why? We dont wanna turn the player vertically only camera.
        float xRot           = Input.GetAxisRaw("Mouse Y");
        float cameraRotation = xRot * lookSensivility;

        //Apply
        motor.RotateCamera(cameraRotation);

        Vector3 jump = Vector3.zero;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            jump = Vector3.up * jumpForce;
            SetJointSettings(0f);
        }
        else
        {
            SetJointSettings(jointSpring);
        }

        motor.ApplyJump(jump);
    }
    // Update is called once per frame
    void Update()
    {
        float dt = Time.deltaTime;

        //Get axis

        if (canFly)
        {
            /*inputX = Input.GetAxisRaw("Horizontal");
             * Vector3 movFrontal = transform.forward * speed;
             *
             * //final movement vector
             * Vector3 velocity = (movFrontal) * speed;
             * //velocity -= 1f * Vector3.up;
             * //apply movement
             * motor.Move(velocity);*/
            float   yRot           = Input.GetAxisRaw("Mouse X");
            float   xRot           = Input.GetAxisRaw("Mouse Y");
            float   cameraRotation = xRot * lookSensivility;
            Vector3 rotationPlayer = new Vector3(-xRot, yRot, 0f) * lookSensivility;

            //Apply
            motor.Rotate(rotationPlayer);

            //Rotation VERTICAL, we will turn the camera in a vertical axis, why? We dont wanna turn the player vertically only camera.


            //Apply
            motor.RotateCamera(cameraRotation);
            //Rotation with acceleration

            /*Vector3 targetRotationSpeed = new Vector3(angularSpeed * inputY, angularSpeed * inputX, 0f);
             * Vector3 angularVelOffset = targetRotationSpeed - angularVel;
             * angularVelOffset = Vector3.ClampMagnitude(angularVelOffset, angularAcceleration * dt);
             * angularVel += angularVelOffset;
             * transform.eulerAngles += angularVel;*/


            //Movement with acceleration
            Vector3 targetSpeed = transform.forward * speed;
            Vector3 velOffset   = targetSpeed - vel;
            velOffset   = Vector3.ClampMagnitude(velOffset, acceleration * dt);
            vel        += velOffset;
            rb.velocity = vel;
        }
        else
        {
            //motor.Move(Vector3.zero);
            motor.Rotate(Vector3.zero);
            if (jumping)
            {
                //if the gameObject stoped to jump and start to fall, then start to move again
                if (lastY > transform.position.y)
                {
                    canFly  = true;
                    jumping = false;
                    //contrain rotation, not let the plane go crazy if colides
                    rb.constraints = RigidbodyConstraints.FreezeRotation;
                }
            }
            else
            {
                //start to decelerate
                if (rb.velocity.magnitude > 0.1)
                {
                    rb.AddForce(-rb.velocity * 1 / maxSpeed);
                    vel = rb.velocity;
                }
                else
                {
                    rb.velocity = Vector3.zero;
                    vel         = Vector3.zero;
                }
            }
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (!canFly && !jumping)
            {
                jump();
            }
        }
        lastY = transform.position.y;
    }