コード例 #1
0
        private void Update()
        {
            if (body)
            {
                // Wheel turning

                BeetleControls controls = Game.BEETLE_CONTROLS[index];

                if (controls.useGamepad)
                {
                    turn.target = Input.GetAxis(controls.hor);
                }

                else
                {
                    turn.target = (Input.GetKey(controls.right) ? 1 : 0)
                                  - (Input.GetKey(controls.left) ? 1 : 0);
                }

                Quaternion turnRot = Quaternion.Euler(0, turn * 15, 0);
                wheelFrontLeft.localRotation = wheelFrontRight.localRotation = turnRot;

                // Velocity jiggle

                Vector3 diff = velocityPrev - body.velocity;
                velocityPrev = body.velocity;

                scale.velocity += diff.y * 0.0125f;
                scale.velocity -= Mathf.Abs(diff.x) * 0.0025f;
                scale.velocity -= Mathf.Abs(diff.z) * 0.0025f;
            }

            transform.localScale = new Vector3(1 - scale, 1 + scale, 1 - scale);
        }
コード例 #2
0
        // ====================================================================================================================

        private void FixedUpdate()
        {
            // Read input (using the controls for this beetle's index)

            float h = 0;
            float v = 0;

            if (manager.AllowInput)
            {
                BeetleControls controls = Game.BEETLE_CONTROLS[index];

                if (controls.useGamepad)
                {
                    h = Input.GetAxisRaw(controls.hor);
                    v = Input.GetAxisRaw(controls.ver);
                }

                else
                {
                    keycodeSteering.target = (Input.GetKey(controls.right) ? 1f : 0f)
                                             - (Input.GetKey(controls.left) ? 1f : 0f);

                    h = keycodeSteering;
                    v = (Input.GetKey(controls.up) ? 1f : 0f)
                        - (Input.GetKey(controls.down) ? 1f : 0f);
                }
            }

            // ====================================================================================

            // Apply turn with horizontal input and based on the current velocity

            float turn = Vector3.Dot(body.velocity, transform.forward) * h * turnForce;

            body.AddRelativeTorque(0, turn, 0, ForceMode.Acceleration);

            // Apply torque to keep kart upright

            Quaternion r        = Quaternion.FromToRotation(transform.up, Vector3.up);
            Vector3    righting = new Vector3(r.x, r.y, r.z) * rightingForce;

            body.AddTorque(righting, ForceMode.Acceleration);

            // Apply angular drag

            body.angularVelocity *= turnDrag;

            // ====================================================================================

            // Apply thrust with vertical input

            Vector3 thrust = transform.forward * v * thrustForce;

            body.AddForce(thrust, ForceMode.Acceleration);

            // Apply drag (separated orthogonally)

            Vector3 vF = Vector3.Project(body.velocity, transform.forward);
            Vector3 vL = Vector3.Project(body.velocity, transform.right);
            Vector3 vV = body.velocity - vF - vL;

            body.velocity = (vF * thrustForwardDrag) + (vL * thrustLateralDrag) + vV;

            // ====================================================================================

            // Store speed

            speedPrev = body.velocity.magnitude;
        }