Exemplo n.º 1
0
 public void Acceleration()
 {
     animator.SetLayerWeight(animator.GetLayerIndex("Acelerar"), Mathf.Clamp01(inputManager.Accelerate()));
     animator.SetLayerWeight(animator.GetLayerIndex("Frenar"), Mathf.Clamp01(-inputManager.Accelerate()));
 }
Exemplo n.º 2
0
    public void Controller()
    {
        Camera.SetupCurrent(myCamera);

        //convertimos la velocidad de global a local
        Vector3 locVel = modelTransform.InverseTransformDirection(rb.velocity);

        GetComponent <NaveAnimationManager>().move = locVel.z != 0;
        //depende de la velocidad la camara esta mas o menos cerca del coche
        //myCamera.gameObject.GetComponent<CameraController>().velocityOffset = new Vector3(0, 0, Mathf.Clamp(locVel.z / (GetComponent<Maneuverability>().currentVelocity / 15), minCameraOffset, maxCameraOffset));
        myCamera.fieldOfView = Mathf.Lerp(myCamera.fieldOfView, fieldOfView + Mathf.Clamp(locVel.z * (inBoost ? 2 : 1) / 15f, 0f, 80f), Time.deltaTime);



        if (!inDrift)
        {
            //disminuimos poco a poco la velocidad lateral para que no se vaya demasiado la nave
            locVel.x *= 0.95f;
            if (Mathf.Abs(locVel.x) < 2f)
            {
                locVel = new Vector3(0, locVel.y, locVel.z);
            }
        }


        Ray        ray = new Ray();
        RaycastHit hit;

        ray.origin    = transform.position;//+ new Vector3(0, 0, Mathf.Clamp(locVel.z / (velocity / 10), -6f, 6f));
        ray.direction = -Vector3.up;

        //si el vehiculo esta cerca del suelo
        if (Physics.Raycast(ray, out hit, maneuverHeight, LayerMask.GetMask("Floor")))
        {
            //mover hacia adelante
            if (inputManager.Accelerate() > 0)
            {
                if (locVel.z < 0) // si estas moviendote hacia atras y quieres ir hacia adelante se ayuda a parar el vehiculo
                {
                    locVel = new Vector3(locVel.x, locVel.y, locVel.z * (1 - (friction)));
                }
                if (inDrift)                               //si la nave esta derrapando
                {
                    if (inputManager.MainHorizontal() > 0) //si esta girando hacia la derecha
                    {
                        if (locVel.x > 0)                  //si la velocidad lateral hacia la derecha es positiva se pone a 0
                        {
                            locVel.x = 0;
                        }
                        //impulso hacia delante, más pequeño que cuando no esta derrapando
                        rb.AddForce(modelTransform.forward * 0.2f * inputManager.Accelerate() * GetComponent <Maneuverability>().AcelerationWithWeight *Time.deltaTime, ForceMode.VelocityChange);
                        //impulso lateral hacia el lado contrario que se esta girando
                        Quaternion rot = modelTransform.rotation;
                        modelTransform.rotation = Quaternion.Euler(modelTransform.eulerAngles.x, modelTransform.eulerAngles.y, 0);
                        rb.AddForce(-modelTransform.right * driftVelocity * inputManager.Accelerate() * GetComponent <Maneuverability>().AcelerationWithWeight *Time.deltaTime, ForceMode.VelocityChange);
                        modelTransform.rotation = rot;
                    }
                    else if (inputManager.MainHorizontal() < 0)   //si esta girando hacia la izquierda
                    {
                        if (locVel.x < 0)
                        {
                            locVel.x = 0;
                        }
                        //impulso hacia delante, más pequeño que cuando no esta derrapando
                        rb.AddForce(modelTransform.forward * 0.2f * inputManager.Accelerate() * GetComponent <Maneuverability>().AcelerationWithWeight *Time.deltaTime, ForceMode.VelocityChange);
                        //impulso lateral hacia el lado contrario que se esta girando
                        Quaternion rot = modelTransform.rotation;
                        modelTransform.rotation = Quaternion.Euler(modelTransform.eulerAngles.x, modelTransform.eulerAngles.y, 0);
                        rb.AddForce(modelTransform.right * driftVelocity * inputManager.Accelerate() * GetComponent <Maneuverability>().AcelerationWithWeight *Time.deltaTime, ForceMode.VelocityChange);
                        modelTransform.rotation = rot;
                    }
                    else
                    {
                        //rb.AddForce(transform.forward * Input.GetAxis("Nave Vertical") * Mathf.Pow(aceleration, 2) * Time.deltaTime, ForceMode.Impulse);
                    }
                }
                else
                {
                    rb.AddForce(modelTransform.forward * inputManager.Accelerate() * GetComponent <Maneuverability>().AcelerationWithWeight *Time.deltaTime, ForceMode.VelocityChange); //fuerza para moverte hacia adelante
                }
            }
            else if (inputManager.Accelerate() < -0.5f) //mover hacia atras
            {
                if (locVel.z > 0)                       // si estas moviendote hacia adelante y quieres ir hacia atras se ayuda a parar el vehiculo
                {
                    locVel = new Vector3(locVel.x, locVel.y, locVel.z * (1 - (friction)));
                }
                rb.AddForce(modelTransform.forward * inputManager.Accelerate() * GetComponent <Maneuverability>().AcelerationWithWeight *backwardVelocity *Time.deltaTime, ForceMode.VelocityChange);  // fuerza para moverte hacia atras
            }



            //rotación al girar

            if (locVel.z >= -0.2f)
            {
                modelTransform.localRotation = Quaternion.Euler(modelTransform.localRotation.eulerAngles.x, modelTransform.localRotation.eulerAngles.y + (inputManager.MainHorizontal() * GetComponent <Maneuverability>().currentManeuver *Time.deltaTime), modelTransform.localRotation.eulerAngles.z);
            }
            else if (locVel.z < -0.2f)
            {
                modelTransform.localRotation = Quaternion.Euler(modelTransform.localRotation.eulerAngles.x, modelTransform.localRotation.eulerAngles.y - (inputManager.MainHorizontal() * GetComponent <Maneuverability>().currentManeuver *Time.deltaTime), modelTransform.localRotation.eulerAngles.z);
            }


            //derrape
            if (inputManager.Drift())
            {
                if (locVel.z > 0)
                {
                    inDrift = true;
                    myCamera.gameObject.GetComponent <CameraController>().cameraDampingMultiplayer = 0.3f;
                }
                else
                {
                    myCamera.gameObject.GetComponent <CameraController>().cameraDampingMultiplayer = Mathf.Lerp(myCamera.gameObject.GetComponent <CameraController>().cameraDampingMultiplayer, 1f, Time.deltaTime);
                    inDrift = false;
                }
            }
            else
            {
                myCamera.gameObject.GetComponent <CameraController>().cameraDampingMultiplayer = Mathf.Lerp(myCamera.gameObject.GetComponent <CameraController>().cameraDampingMultiplayer, 1f, Time.deltaTime);
                inDrift = false;
            }


            //si no se estan pulsando las teclas que hacen moverse al vehiculo
            if (!AnyMovementKeys)
            {
                locVel = new Vector3(locVel.x, locVel.y, locVel.z * (1 - (friction))); //se ralentiza el vehiculo
                //locVel = new Vector3(locVel.x, locVel.y, locVel.z - locVel.z * 0.02f);
                if (Mathf.Abs(locVel.z) < 2f)
                {
                    locVel = new Vector3(locVel.x, locVel.y, 0f);
                }
            }
        }
        else //si el vehiculo no esta cerca del suelo se añade una fuerza para que caiga más rapido (de lo contrario tarda mucho en caer)
        {
            myCamera.gameObject.GetComponent <CameraController>().cameraDampingMultiplayer = 1f;
            inDrift = false;
        }



        rb.angularVelocity = Vector3.zero;

        //rotación lateral al girar
        modelTransform.localEulerAngles = new Vector3(modelTransform.localEulerAngles.x, modelTransform.localEulerAngles.y, Mathf.LerpAngle(modelTransform.localEulerAngles.z, Mathf.Clamp(maxInclination * -inputManager.MainHorizontal() * (rb.velocity.magnitude / VelocityFormula) * (GetComponent <Maneuverability>().currentManeuver / 100), -maxInclination, maxInclination), Time.deltaTime * rotationDamping));

        //si no se esta girando hece que el vehiculo deje de rotar
        if (inputManager.MainHorizontal() == 0)
        {
            rb.angularVelocity = new Vector3(rb.angularVelocity.x, 0, rb.angularVelocity.z);
        }

        //rb.angularVelocity = new Vector3(rb.angularVelocity.x, rb.angularVelocity.y*0.95f, rb.angularVelocity.z);

        //controlamos la velocidad no vertical para ponerle un tope
        Vector2 notVerticalVel = new Vector2(locVel.x, locVel.z);

        //si la velocidad no vertical supera la velocidad maxima del vehiculo la bajamos hasta la velocidad maxima
        if (notVerticalVel.magnitude > VelocityFormula)
        {
            Vector2 correctedVel = notVerticalVel.normalized * VelocityFormula;

            //locVel = Vector3.Lerp( locVel,new Vector3(correctedVel.x, locVel.y, correctedVel.y),Time.deltaTime*1000);
            locVel = new Vector3(correctedVel.x, locVel.y, correctedVel.y);
        }


        //convertimos la velocidad local en la velocidad global y la aplicamos
        rb.velocity = modelTransform.TransformDirection(locVel);
        if (modelTransform.forward.y < maxSubida && modelTransform.forward.y > 0)
        {
            rb.AddForce(-Physics.gravity * modelTransform.forward.y * GetComponent <Maneuverability>().AcelerationWithWeight, ForceMode.Acceleration);
        }
    }
Exemplo n.º 3
0
    private void Controller()
    {
        //se actualiza el valor de las variables de rotación
        float xRotation = GetComponent <NaveController>().modelTransform.localEulerAngles.x;
        float zRotation = GetComponent <NaveController>().modelTransform.localEulerAngles.z;

        if (GetComponent <NaveManager>().isPlanning)
        {
            //si el ángulo esta dentro del límite establecido y se tocan las teclas la nave se rota
            if ((xRotation <= maxXAngle + 0.1f && xRotation >= 0) || (xRotation <= 360 && xRotation >= 360 - 0.1f + minXAngle))
            {
                xRotation += inputManager.MainVertical() * Time.deltaTime * xSensivility * (verticalInverted ? -1 : 1);
                xRotation  = ClampAngle(xRotation, minXAngle, maxXAngle);
            }
            if ((zRotation <= maxZAngle + 0.1f && zRotation >= 0) || (zRotation <= 360 && zRotation >= 360 - 0.1f + minZAngle))
            {
                zRotation -= inputManager.MainHorizontal() * Time.deltaTime * zSensivility;
                zRotation  = ClampAngle(zRotation, minZAngle, maxZAngle);
            }
        }



        //si no se estan tocando las teclas la rotación vuelve a 0
        if (inputManager.MainHorizontal() == 0 || !GetComponent <NaveManager>().isPlanning)
        {
            zRotation = Mathf.LerpAngle(zRotation, 0, Time.deltaTime);
        }


        if (GetComponent <NaveManager>().isPlanning)
        {
            if (inputManager.MainVertical() == 0)
            {
                xRotation = Mathf.LerpAngle(xRotation, defaultXRotation, Time.deltaTime);
            }
        }
        else
        {
            xRotation = Mathf.LerpAngle(xRotation, 0, Time.deltaTime);
        }


        //modificamos la rotación del objeto
        GetComponent <NaveController>().modelTransform.localRotation = Quaternion.Euler(xRotation, GetComponent <NaveController>().modelTransform.localEulerAngles.y, zRotation);



        if (GetComponent <NaveManager>().isPlanning)
        {
            //añadimos la fuerza hacia delante
            rb.AddForce(GetComponent <NaveController>().modelTransform.forward *GetComponent <Maneuverability>().AcelerationWithWeight *Mathf.Clamp01(inputManager.Accelerate()) * Time.deltaTime, ForceMode.VelocityChange);


            //movimiento lateral
            Vector3 lateralForce = GetComponent <NaveController>().modelTransform.right;
            lateralForce = new Vector3(lateralForce.x, 0, lateralForce.z).normalized;
            print(lateralForce);
            rb.AddForce(GetComponent <NaveController>().modelTransform.right *lateralVelocity *inputManager.MainHorizontal());

            rb.AddForce(lateralForce * inputManager.MainHorizontal() * lateralVelocity * GetComponent <Maneuverability>().AcelerationWithWeight *Time.deltaTime, ForceMode.VelocityChange);
            //girar
            GetComponent <NaveController>().modelTransform.localRotation = Quaternion.Euler(GetComponent <NaveController>().modelTransform.localRotation.eulerAngles.x, GetComponent <NaveController>().modelTransform.localRotation.eulerAngles.y + (inputManager.MainHorizontal() * GetComponent <Maneuverability>().maneuver *maneuverLimitator *Time.deltaTime), GetComponent <NaveController>().modelTransform.localRotation.eulerAngles.z);

            RegulateVelocity();
        }
    }