Beispiel #1
0
    private void FixedUpdate()
    {
        float horizontal = Input.GetAxis(_keys.HorizontalAxis);
        float vertical   = Input.GetAxis(_keys.VerticalAxis);

        _hasInput = horizontal > 0.00001f || vertical > 0.000001f;
        _playerShip.SetEnginesOn(_hasInput);

        Rigidbody rb = Rigidbody;

        float speedFactor   = GetSpeedFactor();
        float rotationSpeed = speedFactor * _rotationSpeed;
        float currentSpeed  = speedFactor * _speed;

        Quaternion rotation = transform.rotation * Quaternion.Euler(new Vector3(0.0f, rotationSpeed * horizontal, 0.0f));

        rb.MoveRotation(rotation);

        //Vector3 movement = new Vector3 (horizontal, 0.0f, vertical);
        //TODO fix: move towards rotation head
        //Vector3 movement = new Vector3 (0.0f, 0.0f, vertical);
        //let's remove the velocities in all other directions as the movement direction
        //rb.velocity = new Vector3(0.0f, 0.0f, rb.velocity.z);

        Vector3 position = rb.position;

        position += transform.forward * Time.deltaTime * currentSpeed * vertical;
        position  = Vector3.Scale(new Vector3(1.0f, 0.0f, 1.0f), position);

        // Limit the position to stay inside the game area. not 100% mathematically correct but good enough
        if (ShipCollider != null)
        {
            Vector3 shipExtents = 1.1f * ShipCollider.bounds.extents;            //works without the factor, but put it just-in-case

            float xMin = -1 * (_transformLimits.localScale.x / 2) + shipExtents.x;
            float xMax = (_transformLimits.localScale.x / 2) - shipExtents.x;
            float zMin = -1 * (_transformLimits.localScale.z / 2) + shipExtents.z;
            float zMax = (_transformLimits.localScale.z / 2) - shipExtents.z;
            position = new Vector3(
                Mathf.Clamp(position.x, xMin, xMax),
                0.0f,
                Mathf.Clamp(position.z, zMin, zMax));
        }

        rb.MovePosition(position);
    }