コード例 #1
0
    // Update is called once per frame
    private void Update()
    {
        if (!photonView.isMine)
        {
            return;
        }

        // Set controllable for current character
        if (!_controlling && PhotonNetwork.playerName == this.gameObject.GetComponent <Character>().UserName)
        {
            SetControllable();
            _controlling = true;
        }

        // Detect user input of movement
        GameObject joyStick = GameObjectFinder.FindJoyStick();

        if (joyStick == null)
        {
            return;
        }

        // Handle joystick input to move the character
        VirtualJoyStick vjs = joyStick.GetComponent <VirtualJoyStick>();
        Vector3         joyStickMovement = vjs.GetStickPosition();

        if (joyStickMovement != Vector3.zero)
        {
            _rb.AddForce(joyStickMovement * VELOCITY, ForceMode.Acceleration);
        }
        _rb.velocity = Vector3.ClampMagnitude(_rb.velocity, MAX_VELOCITY);

        // Handle joystick input to rotate the character towards
        // the same direction as the joystick
        if (!joyStickMovement.Equals(Vector3.zero))
        {
            Vector3 targetDir = joyStickMovement;
            float   step      = 10;
            Vector3 newDir    = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
            _lastRotation      = Quaternion.LookRotation(newDir);
            transform.rotation = _lastRotation;
            photonView.RPC("PlayAnim", PhotonTargets.All, "Move|Move");
        }
        else
        {
            transform.rotation = _lastRotation;
            photonView.RPC("PlayAnim", PhotonTargets.All, "Move|Idle");
        }
    }