예제 #1
0
    void keyboardUpdate()
    {
        GraGUI gg = gameObject.GetComponent<GraGUI> ();
        Headbobber cbob = GameObject.Find("PlayerCam").GetComponent<Headbobber> ();
        GrenadeThrow gt = gameObject.GetComponent<GrenadeThrow> ();

        us = gg.us;
        canJump = true;
        float vScale = 1.5f;
        speed = normalSpeed;
        xVel = GetComponent<Rigidbody>().velocity.magnitude;
        yVel = GetComponent<Rigidbody>().velocity.y;

        if (grounded) {
            if (padjump == true){
                timer-=Time.deltaTime;
                if (timer < 0){
                    padjump = false;
                    timer = 0.6f;
                }
            }
            //Debug.Log(padjump);
            //USTAWIENIA STAMINY
            if (stamina>1 && restTime>=100){
                speed=normalSpeed;
                canSprint=true;
            }

            else if (stamina<15) {
                speed=tiredSpeed;
                canSprint=false;
                restTime=1;
            }

            if (GetComponent<Rigidbody>().velocity.magnitude<3 && stamina<=100){
                stamina+=0.5f;
                if (restTime<100) restTime+=1;
            }
            else if (speed==normalSpeed && stamina<=100){
                stamina+=0.025f;
                if (restTime<100) restTime+=1;
            }
            else if (speed==crchSpeed && stamina<=100){
                stamina+=0.2f;
                if (restTime<100) restTime+=1;
            }
            else if (speed==tiredSpeed && stamina<=100){
                stamina+=0.15f;
                if (restTime<100) restTime+=1;
            }
            //
            //WYCHYLANIE SIE (do poprawki)
            /*if(Input.GetKeyDown("q")) {
                cbob.lean=-3;

            }
            if(Input.GetKeyDown("e")) {
                cbob.lean=3;

            }
            if(Input.GetKeyDown("b")) {
                cbob.lean=0;

            }*/
            if (us.GetKey("crouch"))
            { 	vScale = 0.85f;}
            if (grounded && us.GetKey("up") || grounded && us.GetKey("right") || grounded && us.GetKey("down") || grounded && us.GetKey("left"))
            {
                footstepDelay = 0.6f;
                stepVol = 0.7f;
                nextFootstep -= Time.deltaTime;
                if (us.GetKey("run") && us.GetKey("up") && canSprint == true){//SPRINT
                    footstepDelay = 0.5f;
                    speed = runSpeed; //szybkosc poruszania sie w biegu
                    stamina-=0.8f;
                    stepVol = 0.85f;
                }
                else if (us.GetKey("crouch"))
                { 	footstepDelay = 0.7f;
                    //				crouch = true;
                    canJump = false;
                    //bodyCollider.height = 10;

                    stepVol = 0.25f;
                    speed = crchSpeed; //szybkosc poruszania sie w przykucnieciu
                }
            }

            if (nextFootstep <= 0) {
                GetComponent<AudioSource>().PlayOneShot(stepSound, stepVol);
                nextFootstep += footstepDelay;
            }
            //BIEG
            Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            targetVelocity = transform.TransformDirection(targetVelocity);
            targetVelocity *= speed;
            //Debug.Log (rigidbody.velocity.magnitude);

            // Apply a force that attempts to reach our target velocity
            Vector3 velocity = GetComponent<Rigidbody>().velocity;
            Vector3 velocityChange = (targetVelocity - velocity);
            velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange)*sidesSpeedModifier;
            velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange)*forwardSpeedModifier; //
            velocityChange.y = 0;
            GetComponent<Rigidbody>().AddForce(velocityChange, ForceMode.VelocityChange);
            //
            //kucanie - do poprawki, glowa nie moze "kucac", a obecnie kuca. W ogole glowa moze byc do usuniecia, tylko wtedy nalezaloby zrobic osobne hitboxy glowy
            float ultScale = tr.localScale.y; // crouch/stand up smoothly

            Vector3 tmpScale = tr.localScale;
            Vector3 tmpPosition = tr.position;

            tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 20 * Time.deltaTime);
            tr.localScale = tmpScale;

            tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position
            tr.position = tmpPosition;
            //
            //
            //SKOK

            if (canJump && (us.GetKey("jump") || Input.GetMouseButtonDown(1))) {
                GetComponent<Rigidbody>().velocity = new Vector3(velocity.x*jumpSpeedModifier, CalculateJumpVerticalSpeed(), velocity.z*jumpSpeedModifier);
                source.PlayOneShot(jump_startSound,1f);
                canJump = false;
            }

            //UPADEK z duzej wysokosci
            if (falldmg<-75)
            {
                //Debug.Log(falldmg*0.35);
                falldmg*=-0.35f;
                vScale = 0.8f;
                //	float asd = cbob.midpoint;
                getDmg((int)falldmg);
                source.PlayOneShot(fallSound,1f);
                //GUI.color.a=1f; //
                alpha=0.5f;
                blood=true;
                cbob.midpoint=1.1f;

                //	Debug.Log(asd);
                //	cbob.midpoint=1; ruch kamery przy upadku
                falldmg=0;

            }

            //usuwanie krwi z ekranu
            if (blood == true && alpha >0){
                alpha -= Time.deltaTime/4;
                if (cbob.midpoint <2.0f){cbob.midpoint += Time.deltaTime*1.5f;}
                if (cbob.midpoint >2.0f) {cbob.midpoint = 2.0f;}
                if (alpha <=0){ blood = false;}

            }
            //

            //
            grounded = false;

            if (us.GetKeyDown("granade")) //GRANAT
            {
                gt.Throw();

            }
        }
        else // not grounded

        {	//upadek
            if(GetComponent<Rigidbody>().velocity.y < -50.0 && padjump == false)
            {	falldmg=GetComponent<Rigidbody>().velocity.y;
            }

            // poruszanie sie w locie

            Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            if (padjump == true)
            {
                inAirControl = 0.8f;
            }
            else {inAirControl = 0.4f;}
            targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;
            GetComponent<Rigidbody>().AddForce(targetVelocity, ForceMode.VelocityChange);

        }
        // We apply gravity manually for more tuning control
            GetComponent<Rigidbody>().AddForce(new Vector3 (0, -gravity * GetComponent<Rigidbody>().mass, 0));

        // PC: double jump
        if ((us.GetKeyDown("jump") || Input.GetMouseButtonDown(1)) && doubleJumped == false) {
            doubleJumped = true;
            Vector3 velocity = GetComponent<Rigidbody>().velocity;
            GetComponent<Rigidbody>().velocity = new Vector3(velocity.x*jumpSpeedModifier, CalculateJumpVerticalSpeed(), velocity.z*jumpSpeedModifier);
            source.PlayOneShot(jump_startSound,1f);
        }

        // PC: teleportowanie
        if (us.GetKeyDown("teleport") && canUseTeleport) {
            //rigidbody.velocity = Vector3.zero;
            // to naprawia kolizje jak player jest bardzo szybki
            GetComponent<Rigidbody>().collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
            /*Vector3 targetVelocity = transform.TransformDirection(0, 0, 190);
            rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
        */
            padjump = true; //zeby gracz nie otrzymywal obrazen jak teleportuje sie z gory, z wysoka na dol
            source.PlayOneShot(teleSound,1f);
            GameObject cam = transform.FindChild ("PlayerCam").gameObject;

            //Ray ray = cam.camera.ScreenPointToRay(Input.mousePosition);
            Ray ray = cam.GetComponent<Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
            //Vector3 targetVelocity = transform.TransformDirection(ray.direction.x * 190, ray.direction.y * 190, ray.direction.z * 190);
            //rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
            //transform.position = ray.origin + ray.direction * 75;
            RaycastHit hitInfo = new RaycastHit();
            bool traf = Physics.Raycast(ray, out hitInfo);

            if(traf) {
                // takie cos dziala ale instant teleport, w sumie nie wiem czy pan W tak nie chcial
                //transform.position = hitInfo.point;

                // eskperymenty
                /// nie wiem jak to zrobic ok, bo na cialo ma wplyw takze grawitacja i inne smieszne rzeczy
                /// jakby nie miala to chyba by cos takiego (albo podobnego) smigalo
                /// ale musimy teraz nasza sila przeciwdzialac grawitacji i nie wiem jak to zrobic :/
                //Vector3 sila = (hitInfo.normal) * hitInfo.distance;
                //print (hitInfo.normal);
                //print ((hitInfo.point - transform.position).normalized);
                //rigidbody.AddForce(sila, ForceMode.VelocityChange);
                //rigidbody.transform.position = Vector3.MoveTowards(rigidbody.transform.position, hitInfo.point, 40);

                canUseTeleport = false;
                timerTeleport.Stop();
                timerTeleport.Start();
                teleportOpacity = 0.4341231233f;
                timerTeleportAnim.Stop();
                timerTeleportAnim.Start();
                teleportPos = hitInfo.point;
                teleportPos.y += 2; // workaround buga wchodzenie w sufity

            }
            else {
                teleportPos = Vector3.zero;
            }

        }
        if (this.transform.position.y<0 && allow==true){
            Die ();
            allow = false;
        }
    }