예제 #1
0
        public Vector3 get()
        {
            Vector3 vPos = new Vector3(Random.Range(position.x - radius, position.x + radius), 0, Random.Range(position.z - radius, position.z + radius));

            vPos.y = HeightRaycaster.GroundY(vPos);

            return(vPos + Vector3.up);
        }
    // Update is called once per frame
    private void FixedUpdate()
    {
        if (Client.Instance != null)
        {
            /*
             * If there is no players in vehicle, stop the vehicle
             * */
            if (vehicle.p.Count == 0 || vehicle.p[0] == 0)
            {                                                                               // No driver
                info.m = Mathf.Clamp(info.m - Time.deltaTime * 100, 0, 1 / maxMotorTorque); // Slow down gas
                info.a = 0;                                                                 // Reset the steering
                info.b = 0;                                                                 // Let's get land.
            }
        }

        /// SAMPLE HELICOPTER CODE IS STARTING ///

        /// Calculating ground distance for helicopter.
        float groundDist = HeightRaycaster.DistToGround(transform.position);

        if (isLocalVehicle)
        {
            /*
             * Only the driver can control the vehicle
             * *
             */

            if (InputBlocker.isBlocked())
            {
                // If we are on a UI Input.
                info.m = 0;
                info.a = 0;
                info.b = 0;
            }
            else
            {
                /*
                 * USE THE HELICOPTER
                 * */
                float vert = Input.GetAxis("Vertical");
                if (vert > 0 && groundDist > 1) // 1m is for waiting for rising.
                {
                    info.m = maxMotorTorque * vert;
                }
                else
                {
                    info.m = 0;
                }

                info.a = steeringPower * Input.GetAxis("Horizontal");

                if (vert < 0) // descending
                {
                    info.b = vert * risingPower / 2f;
                }
                else
                {
                    info.b = Mathf.Abs(Input.GetAxis("Jump")) * risingPower;
                }
            }

            /*
             * THIS IS IMPORTANT, DRIVER MUST SYNC THE VEHICLE ON NETWORK.
             * */
            if (sync < Time.time)
            {
                sync = Time.time + 0.1f; // Sync rate is ~10 times in one second.
                SNet_Network.instance.Send_Message(info);
            }

            /*
             * */
        }

        identity.transform.Rotate(new Vector3(0, info.a * Time.deltaTime, 0));

        Quaternion tRot = new Quaternion();

        tRot.eulerAngles = new Vector3((info.m > 0) ? 5 : 0, transform.eulerAngles.y, -info.a);

        if (info.m > 0 || groundDist > distanceOffset) // Restore the axises
        {
            identity.transform.rotation = Quaternion.Slerp(transform.rotation, tRot, 0.05f);

            /*
             * THIS IS FOR ANIMATION
             * */
            fanTop.Rotate(fanTopRotateAxis, 720 * Time.deltaTime);

            /*
             * */
        }

        /// Move the helicopter.
        Vector3 force = transform.forward * info.m + Vector3.up * info.b;

        transform.position = identity.rbody.rBody.position + force * Time.deltaTime;

        /// SAMPLE HELICOPTER CODE IS ENDED ///

        /// Call this end of the update loop to update the passengers on vehicle.
        ControlPassengers();
    }
예제 #3
0
    void Update()
    {
        isGrounded = HeightRaycaster.isGrounded(transform.position);

        /// We send a ray towards from our position but y += 1.25f, if it hits something walkable, we cannot move.
        /// This is the slope fix.
        canMove = !Physics.Raycast(transform.position + Vector3.up * 1.25f, transform.forward, 2f, HeightRaycaster.ground);

        if (isDead)
        {
            return; // Dead players cannot use inputs. Free camera must be activated
        }
        if (currentVehicle != null && !freeOnSeat)
        {
            //We are on a vehicle, also we are not free on the current seat.
            return;
        }

        if (!isLocalPlayer)
        {
            /* For other players, aiming must be smooth.
             * Smooth Aim Sync
             * */
            if (syncedAim != null)
            {
                aim.y = Mathf.Lerp(aim.y, syncedAim.y, 0.1f);
            }
            return;
        }

        if (InputBlocker.isBlocked())
        {
            /// Input blocked because of UI InputField
            horizontal = 0;
            vertical   = 0;
            Sync(); // Sync anyway.
            return;
        }

        if (currentVehicle == null)
        { // Even we are on a free set, we cannot move
            horizontal = Mathf.Lerp(horizontal, Input.GetAxis("Horizontal"), Time.deltaTime * 12);
            vertical   = Mathf.Lerp(vertical, Input.GetAxis("Vertical"), Time.deltaTime * 12);

            if (Input.GetButtonDown("Jump") && nextJump < Time.time && (isGrounded || identity.rbody.rBody.velocity.magnitude < 0.1f) && canMove)
            {/// Simple jumping
                nextJump = Time.time + 1;
                identity.rbody.rBody.AddForce(transform.up * 5 + transform.forward * vertical * 2 * (identity.animator.animator.GetBool("Sprint") ? 2 : 1) + transform.right * horizontal * 1, ForceMode.VelocityChange);
                identity.rbody.nextUpdate = Time.time + 0.02f;
            }

            /*
             * Local move input. It will be synced, but not in every frame so we are doing this for local client.
             * */
            identity.animator.animator.SetFloat("H", horizontal);
            identity.animator.animator.SetFloat("V", vertical);
            bool isMoving = (Mathf.Abs(horizontal) > 0.1f || Mathf.Abs(vertical) > 0.1f);
            identity.animator.animator.SetBool("M", isMoving);

            /*
             * */

            // Spriting via Shift key.
            if (Input.GetKeyDown(KeyCode.LeftShift) && vertical > 0.1f)
            {
                identity.animator.SetBool("Sprint", true);
            }
            else if (Input.GetKeyUp(KeyCode.LeftShift) || vertical <= 0.1f)
            { // If we done with shift key, disable the sprint.
                if (identity.animator.animator.GetBool("Sprint"))
                {
                    identity.animator.SetBool("Sprint", false);
                    identity.animator.SetTrigger("ChangeItem"); // We must call this to leave the sprinting state on animator.
                }
            }
        }

        // Rotating our player will rotate the camera because camera is child of our transform.
        transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X"), 0) * Time.deltaTime * sensivityX);
        // Set player aim by Mouse Y
        aim.y = Mathf.Clamp(aim.y - Input.GetAxis("Mouse Y") * sensivityY, -50, 45);

        if (MouseOrbitImproved.instance.target == null)
        {
            /*
             * Set the camera position by aim.
             * */
            Camera.main.transform.localEulerAngles = new Vector3(aim.y, Camera.main.transform.localEulerAngles.y, Camera.main.transform.localEulerAngles.z);
            Vector3 cameraPos = Camera.main.transform.position;
            cameraPos.y = Camera.main.transform.parent.position.y + aim.y / 80;
            Camera.main.transform.position = cameraPos;
        }

        /*
         * Shooting request.
         * */

        if (Input.GetButtonDown("Fire1"))
        {
            isShooting = true;
        }
        else if (Input.GetButtonUp("Fire1"))
        {
            isShooting = false;
        }

        if (isShooting && !identity.animator.animator.GetBool("Sprint") && !SNet_Manager.instance.panel_Lobby.activeSelf)
        {
            if (inventory.inv.ci == null)
            {
                return;
            }

            if (inventory.inv.ci.fireable && (inventory.inv.ci.ammo > 0 || !inventory.inv.ci.countable) && nextFire < Time.time)
            {
                nextFire = Time.time + inventory.inv.ci.fireRate;
                SNet_Network.instance.Send_Message(new PlayerInventory.St());
            }
        }

        Sync(); // Always sync.
    }