Ejemplo n.º 1
0
    private void OnTriggerEnter(Collider other)
    {
        var pickupCollector = other.GetComponent <IPickupCollector>();
        var go = other.gameObject;

        if (pickupCollector == null)
        {
            pickupCollector = other.GetComponentInParent <IPickupCollector>();
            if (pickupCollector != null)
            {
                go = other.transform.parent.gameObject;
            }
        }
        if (pickupCollector != null)
        {
            var playerTno = go.GetComponent <TNObject>();
            if (playerTno.isMine)
            {
                var success = pickupCollector.PickedUp(this);
                if (success)
                {
                    tno.Send("Die", Target.AllSaved);
                }
            }
        }
    }
Ejemplo n.º 2
0
	/// <summary>
	/// Clicking on the object should change its color.
	/// </summary>

	void OnClick ()
	{
		Color color = Color.red;

		if		(GetComponent<Renderer>().material.color == Color.red)	 color = Color.green;
		else if (GetComponent<Renderer>().material.color == Color.green) color = Color.blue;

		TNObject tno = GetComponent<TNObject>();
		tno.Send("OnColor", Target.AllSaved, color);
	}
Ejemplo n.º 3
0
    /// <summary>
    /// Only the car's owner should be updating the movement axes, and the result should be sync'd with other players.
    /// </summary>

    protected override void Update()
    {
        // Only the player that actually owns this car should be controlling it
        if (!tno.isMine)
        {
            return;
        }

        // Update the input axes
        base.Update();

        float time  = Time.time;
        float delta = time - mLastInputSend;
        float delay = 1f / inputUpdates;

        // Don't send updates more than 20 times per second
        if (delta > 0.05f)
        {
            // The closer we are to the desired send time, the smaller is the deviation required to send an update.
            float threshold = Mathf.Clamp01(delta - delay) * 0.5f;

            // If the deviation is significant enough, send the update to other players
            if (Tools.IsNotEqual(mLastInput.x, mInput.x, threshold) ||
                Tools.IsNotEqual(mLastInput.y, mInput.y, threshold))
            {
                mLastInputSend = time;
                mLastInput     = mInput;
                tno.Send("SetAxis", Target.OthersSaved, mInput);
            }
        }

        // Since the input is sent frequently, rigidbody only needs to be corrected every couple of seconds.
        // Faster-paced games will require more frequent updates.
        if (mNextRB < time)
        {
            mNextRB = time + 1f / rigidbodyUpdates;
            tno.Send("SetRB", Target.OthersSaved, mRb.position, mRb.rotation, mRb.velocity, mRb.angularVelocity);
        }
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Only the car's owner should be updating the movement axes, and the result should be sync'd with other players.
    /// </summary>
    protected override void Update()
    {
        // Only the player that actually owns this car should be controlling it
        if (!tno.isMine)
        {
            return;
        }

        if (CheckTouch())
        {
            mInput.y = forwardOrReverse;

            if (!screenIsTouched)
            {
                //Debug.Log("screenPos = " + screenPos.ToString() + ", screen.width = " + Screen.width);
                int screenMiddle = Screen.width / 2;
                mInput.x = (screenPos.x - screenMiddle) / screenMiddle;
            }
            else
            {
                //float myRotY = transform.eulerAngles.y + 360;
                //float camRotY = Camera.main.transform.eulerAngles.y + 360;
                float camRotZ = Camera.main.transform.eulerAngles.z;

                // assuming camRotZ is in [0,360]
                if (camRotZ > 180)
                {
                    mInput.x = -Mathf.Max(-1, (camRotZ - 360) / 20);
                }
                else
                {
                    mInput.x = -Mathf.Min(1, camRotZ / 20);
                }
                //else mInput.x = 0;


                //float diff = camRotY - myRotY;

                if (myText != null)
                {
                    myText.text = "\nCamera eulerAngles = " + Camera.main.transform.eulerAngles.ToString()
                                  + "\ncamRotZ = " + camRotZ + "\nmInput.x = " + mInput.x;
                }

                //myText.text = txt + "\nmy eulerAngles = " + transform.eulerAngles.ToString()
                //    + "\nmyRotY = " + myRotY + "\ncamRotY = " + camRotY
                //    + "\ndiff = " + diff;

                //if (Mathf.Abs(diff) > 1f) mInput.x = Mathf.Sign(diff);

                //mInput.x = -Mathf.Sign(myRotY - camRotY);
                //mInput.x = Camera.main.transform.eulerAngles.z;
            }
        }
        else
        {
            // Update the input axes
            //base.Update();
            mInput.x = Input.GetAxis("Horizontal");
            mInput.y = Input.GetAxis("Vertical");
        }

        float time  = Time.time;
        float delta = time - mLastInputSend;
        float delay = 1f / inputUpdates;

        //return;

        // Don't send updates more than 20 times per second
        if (delta > 1f) //0.05f)
        {
            // The closer we are to the desired send time, the smaller is the deviation required to send an update.
            float threshold = Mathf.Clamp01(delta - delay) * 0.5f;

            // If the deviation is significant enough, send the update to other players
            if (Tools.IsNotEqual(mLastInput.x, mInput.x, threshold) ||
                Tools.IsNotEqual(mLastInput.y, mInput.y, threshold))
            {
                mLastInputSend = time;
                mLastInput     = mInput;
                tno.Send("SetAxis", Target.OthersSaved, mInput);
            }
        }

        // Since the input is sent frequently, rigidbody only needs to be corrected every couple of seconds.
        // Faster-paced games will require more frequent updates.
        if (mNextRB < time)
        {
            mNextRB = time + 1f / rigidbodyUpdates;
            tno.Send("SetRB", Target.OthersSaved, mRb.position, mRb.rotation, mRb.velocity, mRb.angularVelocity);
        }
    }