Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        run = falling = walkBack = strafeR = strafeL = false;

        gamePad.getButtonPressed("ST");

        if (camRotation.isZooming)
        {
            speed       = ADSSpeed;
            strafeSpeed = ADSSpeed;
        }
        else if (sprint)
        {
            speed       = sprintSpeed;
            strafeSpeed = BaseSpeed;
        }
        else
        {
            speed       = BaseSpeed;
            strafeSpeed = BaseSpeed;
        }

        onFloor = Physics.Raycast(transform.position, -camRotation.cam_upVec, out rayOutData, 1.6f);

        //changes Drag if in air
        if (onFloor == false)
        {
            rb.drag = 1;
        }
        else
        {
            rb.drag = 5;
        }

        if (gamePad.isKeyboard == true)
        {
            if (onFloor)
            {                          //WASD movement
                if (Input.GetKey("w")) //forward
                {
                    rb.AddForce(transform.forward * Time.deltaTime * speed, ForceMode.VelocityChange);
                    run = true;
                }
                if (Input.GetKey("s"))//backward
                {
                    speed = 25;
                    rb.AddForce(-transform.forward * Time.deltaTime * speed, ForceMode.VelocityChange);
                    walkBack = true;
                }
                //-----------------------------------------------------------------
                if (Input.GetKey("d"))//right
                {
                    rb.AddForce(camRotation.cam_rightVec * Time.deltaTime * strafeSpeed, ForceMode.VelocityChange);
                    strafeR = true;
                }
                if (Input.GetKey("a"))//left
                {
                    rb.AddForce(-camRotation.cam_rightVec * Time.deltaTime * strafeSpeed, ForceMode.VelocityChange);
                    strafeL = true;
                }
            }
            else
            {
                if (Input.GetKey("w"))//forward
                {
                    rb.AddForce(transform.forward * Time.deltaTime * speed / airRestriction, ForceMode.VelocityChange);
                }
                if (Input.GetKey("s"))//backward
                {
                    rb.AddForce(-transform.forward * Time.deltaTime * speed / airRestriction, ForceMode.VelocityChange);
                }
                //-----------------------------------------------------------------
                if (Input.GetKey("d"))//right
                {
                    rb.AddForce(camRotation.cam_rightVec * Time.deltaTime * strafeSpeed / airRestriction, ForceMode.VelocityChange);
                }
                if (Input.GetKey("a"))//left
                {
                    rb.AddForce(-camRotation.cam_rightVec * Time.deltaTime * strafeSpeed / airRestriction, ForceMode.VelocityChange);
                }
                //falling = true;
            }
        }

        //analog stick movement(Xbox)
        if (gamePad.getGamePadConnected())
        {
            if (onFloor)
            {
                if (gamePad.getAxisRaw("Left", "Y") > 0.2f)
                {
                    run = true;
                }
                if (gamePad.getAxisRaw("Left", "Y") < -0.2f)
                {
                    speed    = 25;
                    walkBack = true;
                }
                if (gamePad.getAxisRaw("Left", "X") > 0.2f)
                {
                    speed   = speed / 2;
                    strafeR = true;
                }
                if (gamePad.getAxisRaw("Left", "X") < -0.2f)
                {
                    speed   = speed / 2;
                    strafeL = true;
                }
                rb.AddForce(transform.forward * Time.deltaTime * speed * gamePad.getAxisRaw("Left", "Y"), ForceMode.VelocityChange);
                rb.AddForce(camRotation.cam_rightVec * Time.deltaTime * strafeSpeed * gamePad.getAxisRaw("Left", "X"), ForceMode.VelocityChange);
            }
            else
            {
                rb.AddForce(transform.forward * Time.deltaTime * speed / airRestriction * gamePad.getAxisRaw("Left", "Y"), ForceMode.VelocityChange);
                rb.AddForce(camRotation.cam_rightVec * Time.deltaTime * strafeSpeed / airRestriction * gamePad.getAxisRaw("Left", "X"), ForceMode.VelocityChange);
                //falling = true;
            }
        }

        // jump
        if (onFloor)
        {
            if ((Input.GetKeyDown("space") && gamePad.isKeyboard) || gamePad.getButtonPressed("A"))
            {
                rb.AddForce(camRotation.cam_upVec * jumpStrength * 2, ForceMode.Force);

                jumping = true;
            }
        }

        if (!onFloor && !jumping)
        {
            falling = true;
        }
        else if (onFloor)
        {
            falling = false;
        }

        if (!gamePad.isKeyboard)
        {
            if (gamePad.getButtonDown("L3"))
            {
                sprint = true;
            }
            else if (Vector2.SqrMagnitude(new Vector2(gamePad.getAxisRaw("Left", "Y"), gamePad.getAxisRaw("Left", "X"))) < 0.2 || playerCamera.GetComponent <Combat_Shoot>().Shot)
            {
                sprint = false;
            }
        }
        else
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                sprint = true;
            }
            else if (!Input.GetKey("w") || playerCamera.GetComponent <Combat_Shoot>().Shot)
            {
                sprint = false;
            }
        }


        //rotates the player to look where camera is looking.
        transform.rotation = camRotation.transform.rotation;
        Vector3 newFwd = Vector3.Cross(camRotation.cam_upVec, camRotation.cam_rightVec);

        transform.rotation = Quaternion.LookRotation(-newFwd, transform.up);
    }
Ejemplo n.º 2
0
 //=====================================================================================================================================================
 void Update()
 {
     if (((Input.GetMouseButton(0) && gamePad.isKeyboard && gun.currentGun.fireType == 0) || (Input.GetMouseButtonDown(0) && gamePad.isKeyboard && gun.currentGun.fireType == 1) || (gun.currentGun.fireType == 0 && gamePad.getButtonDown("RT")) || (gun.currentGun.fireType == 1 && gamePad.getButtonPressed("RT"))) && !Shot) //right mouse click
     {
         Shot  = true;                                                                                                                                                                                                                                                                                                           //passed to gun behaviour
         laser = true;
         if (transform.gameObject.GetComponent <Cam_Raytrace>().rayHit)
         {
             pointHit = transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.point;
             GameObject objectHit = transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.collider.gameObject;
             //sparks.GetComponent<Control>().Sparks(transform.gameObject.GetComponent<Cam_Raytrace>().rayOutData.point,transform.rotation);
             Instantiate <GameObject>(emitter, pointHit, transform.rotation);
             //Debug.Log(objectHit);
             if (objectHit.transform.parent != null)
             {
                 if (objectHit.transform.parent.gameObject.GetComponent <Player_Player>())                                                       // if you're shooting a player
                 {
                     if (objectHit.transform.parent.GetComponent <Player_Player>().GetTeam() != player.GetComponent <Player_Player>().GetTeam()) // if the player is not on your team ========================================================================================================FIX IT
                     {
                         objectHit.transform.parent.GetComponent <Player_Player>().SubFromPlayerHP(gun.currentGun.damage);                       //if object hit is player, remove hp
                         objectHit.transform.parent.GetComponent <Player_Player>().SetLastHitBy(player);
                         objectHit.transform.parent.GetComponent <Player_Player>().regenDelay = 2.5f;
                         objectHit.transform.parent.GetComponent <Rigidbody>().AddForceAtPosition(camRotation.cam_forwardVec * gun.currentGun.force * 6, transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.point, ForceMode.Force);//else if object has rigid body and is not a player, knock back
                         //hit marker stuff
                         Hit = true;
                     }
                 }
             }
             if (objectHit.GetComponent <Rigidbody>())                                                                                                                                                             // if you're shooting a physics object
             {
                 objectHit.GetComponent <Rigidbody>().AddForceAtPosition(camRotation.cam_forwardVec * gun.currentGun.force, transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.point, ForceMode.Force); //else if object has rigid body and is not a player, knock back
             }
         }
     }
 }
Ejemplo n.º 3
0
    void CameraTurning()
    {
        /*if (!isRotating && player.gameObject.GetComponent<Player_Move>().onFloor)
         * {
         *  Debug.Log(Vector3.Angle(player.gameObject.GetComponent<Player_Move>().rayOutData.normal, transform.up));
         *
         *  if (Vector3.Angle(player.gameObject.GetComponent<Player_Move>().rayOutData.normal,transform.up) > 5 && Vector3.Angle(player.gameObject.GetComponent<Player_Move>().rayOutData.normal, transform.up) < 20 && transform.gameObject.GetComponent<Cam_Raytrace>().rayHit)
         *  {
         *
         *      rotationTimer = rotationDuration;
         *      isRotating = true;
         *
         *      old_UpVec = cam_upVec;
         *      // Debug.DrawRay(transform.position, old_UpVec, Color.magenta, 3.0f);
         *      new_UpVec = player.gameObject.GetComponent<Player_Move>().rayOutData.normal;
         *      //Debug.DrawRay(transform.position, new_UpVec, Color.cyan, 3.0f);
         *  }
         * }
         */
        if (isRotating)
        {
            rotationTimer -= Time.deltaTime; //decrement timer
            if (old_UpVec == -new_UpVec)     //to handle when flipping 180 degrees
            {
                old_UpVec += cam_rightVec * 0.001f;
            }
            cam_upVec    = Vector3.Slerp(old_UpVec, new_UpVec, (1.0f - (rotationTimer / rotationDuration)));//lerp between new and old up vectors
            transform.up = cam_upVec;

            cam_rightVec = Vector3.Cross(cam_upVec, transform.forward);//calculate new right vector
            cam_rightVec = Vector3.Normalize(cam_rightVec);
            if (transform.gameObject.GetComponent <Cam_Raytrace>().rayHit)
            {
                transform.LookAt(transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.point, cam_upVec);          //keep camera looking at same angle it had before rotation //===ISSUE HERE: because it's being told to look at a point the raytrace defines, your camera will not change if the raytrace doesn't collide with anything
                NoRecoil.transform.LookAt(transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.point, cam_upVec); //keep camera looking at same angle it had before rotation //===ISSUE HERE: because it's being told to look at a point the raytrace defines, your camera will not change if the raytrace doesn't collide with anything
            }
            if (rotationTimer < 0)                                                                                         //end rotation once timer ends
            {
                isRotating = false;
            }
        }
        if ((Input.GetKeyDown("q") && gamePad.isKeyboard) || gamePad.getButtonPressed("RB"))//right mouse click
        {
            if (transform.gameObject.GetComponent <Cam_Raytrace>().rayHit)
            {
                rotationTimer = rotationDuration;
                isRotating    = true;

                old_UpVec = cam_upVec;
                // Debug.DrawRay(transform.position, old_UpVec, Color.magenta, 3.0f);
                new_UpVec = transform.gameObject.GetComponent <Cam_Raytrace>().rayOutData.normal;
                //Debug.DrawRay(transform.position, new_UpVec, Color.cyan, 3.0f);
            }
        }
        else if (player.GetComponent <Player_Player>().reSpawn)
        {
            rotationTimer = rotationDuration;
            isRotating    = true;

            player.GetComponent <Player_Move>().rb.velocity = Vector3.zero;

            old_UpVec = cam_upVec;
            new_UpVec = player.GetComponent <Player_Player>().newGrav;
            player.GetComponent <Player_Player>().reSpawn = false;
        }
    }