コード例 #1
0
    public void UpdateControls()
    {
        var currentVelocity = gameObject.GetComponent <Rigidbody>().velocity;

        var pointToCheck             = transform.position + currentVelocity * _lookAheadDistance;
        var closestPointOnRacingLine = _racingLine.FindClosest(pointToCheck);

        var heading     = (pointToCheck - closestPointOnRacingLine);
        var varianceMag = heading.magnitude;


        //Determine if car is to the left or right of racing line
        Vector3 perp = Vector3.Cross(transform.forward, heading);
        float   dir  = Vector3.Dot(perp, transform.up);

        Debug.Log(dir);

        if (dir < 0)
        {
            _car.SetTurningPower(varianceMag);
        }
        else
        {
            _car.SetTurningPower(-varianceMag);
        }


        _car.SetAccelerationPower(1);


        _debugPointToCheck = pointToCheck;
        _debugClosestPoint = closestPointOnRacingLine;
    }
コード例 #2
0
    public void UpdateControls()
    {
        var localVelocity = transform.InverseTransformDirection(gameObject.GetComponent <Rigidbody>().velocity);

        if (Input.GetKey(KeyCode.W))
        {
            _car.SetAccelerationPower(1);
        }

        if (Input.GetKey(KeyCode.S))
        {
            if (localVelocity.z > 1)
            {
                _car.SetBrakingPower(1);
            }
            else
            {
                _car.SetAccelerationPower(-0.5f);
            }
        }

        if (Input.GetKey(KeyCode.Space))
        {
            _car.SetBrakingPower(1);
        }

        if (Input.GetKey(KeyCode.A))
        {
            _car.SetTurningPower(-1);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            _car.SetTurningPower(1);
        }
    }