質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Photon Cloud

Photon Cloudは、オンラインゲーム開発向けネットワークエンジン。リアルタイムマルチプレイ通信のプラットフォームであるPhotonの一つです。ネットワーク通信によるオンラインゲームを開発・運営するために必要なツールがサーバー環境に構築されています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

1505閲覧

PUN2 移動すると相手側でガクガクして見える

Jejeje

総合スコア38

Photon Cloud

Photon Cloudは、オンラインゲーム開発向けネットワークエンジン。リアルタイムマルチプレイ通信のプラットフォームであるPhotonの一つです。ネットワーク通信によるオンラインゲームを開発・運営するために必要なツールがサーバー環境に構築されています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/12/25 10:01

実現したいこと

UnityでPUN2を使用したオンラインゲームを制作しております

戦車をジョイスティックで移動させるのですが、移動用のスクリプトを下記のように変更した結果
移動したときに相手側で見るととてつもなくガクガクしながら動いてしまいました。

変更前(move_joystick.cs) → 変更後(move_joystick2.cs)

move_joystick.csでは、キャラクターの向ける方向を、0,45,90,135,180,225,270,315,360度という風に45度単位でしか向けないようにしていました
(例として、ジョイスティックの角度が15 < radian <= 75 のときはキャラクターを45度に向けて前進します)

しかし45度単位でしか角度を変えられないのは操作性が悪くなると考えmove_joystick2.csで、
ジョイスティックの角度と同じ向きに倒せるように変更しました

しかしmove?joystick2.csに変更すると、今まではそうはなっていなかったのがガクガクするようになりました(同期した相手視点で)

何が原因でどのようにしたら改善できるでしょうか
どうか力を貸して欲しいです

変更前の移動スクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6public class move_joystick : MonoBehaviourPun 7{ 8 9 public FixedJoystick joystick; 10 PlayerID script; 11 12 private float speed = 5.0f; 13 private float jumpPower = 275; 14 private float delay = 0f; 15 public AudioClip MoveSound; 16 public AudioClip JumpSound; 17 private Rigidbody rb; 18 19 //public FixedJoystick joystick_clone = Instantiate(joystick , new Vector3(0, 0, 0), Quaternion.identity); 20 21 // Start is called before the first frame update 22 void Start() 23 { 24 25 if (photonView.IsMine) 26 { 27 rb = GetComponent<Rigidbody>(); 28 } 29 30 GameObject UI = GameObject.Find("BattleUI"); 31 script = UI.GetComponent<PlayerID>(); 32 joystick = script.joystick; 33 34 } 35 36 37 // Update is called once per frame 38 void Update() 39 { 40 var x = joystick.Horizontal; 41 var y = joystick.Vertical; 42 float radian = Mathf.Atan2(x, y) * Mathf.Rad2Deg; 43 44 if (radian < 0) 45 { 46 radian += 360; 47 } 48 49 if (photonView.IsMine) 50 { 51 if (x == 0) 52 { 53 if (y == 0) 54 { 55 return; 56 } 57 } 58 59 if (radian <= 15) 60 { 61 62 photonView.RPC(nameof(Move5), RpcTarget.All); 63 Invoke("SoundMove", delay); 64 } 65 66 else if (radian <= 75) 67 { 68 69 photonView.RPC(nameof(Move3), RpcTarget.All); 70 Invoke("SoundMove", delay); 71 } 72 73 else if (radian <= 105) 74 { 75 76 photonView.RPC(nameof(Move1), RpcTarget.All); 77 Invoke("SoundMove", delay); 78 } 79 80 else if (radian <= 165) 81 { 82 83 photonView.RPC(nameof(Move6), RpcTarget.All); 84 Invoke("SoundMove", delay); 85 } 86 87 else if (radian <= 195) 88 { 89 90 photonView.RPC(nameof(Move8), RpcTarget.All); 91 Invoke("SoundMove", delay); 92 } 93 94 else if (radian <= 255) 95 { 96 97 photonView.RPC(nameof(Move7), RpcTarget.All); 98 Invoke("SoundMove", delay); 99 } 100 101 else if (radian <= 285) 102 { 103 104 photonView.RPC(nameof(Move2), RpcTarget.All); 105 Invoke("SoundMove", delay); 106 } 107 108 else if (radian <= 345) 109 { 110 111 photonView.RPC(nameof(Move4), RpcTarget.All); 112 Invoke("SoundMove", delay); 113 } 114 115 else if (radian <= 360) 116 { 117 118 photonView.RPC(nameof(Move5), RpcTarget.All); 119 Invoke("SoundMove", delay); 120 } 121 122 123 Ray ray = new Ray(transform.position, -transform.up); 124 { 125 //接地判定 126 RaycastHit hit; 127 if (Physics.Raycast(ray, out hit, 0.25f)) 128 { 129 if (Input.GetKeyDown(KeyCode.O)) 130 { 131 rb.AddForce(transform.up * jumpPower); 132 AudioSource.PlayClipAtPoint(JumpSound, transform.position); 133 } 134 135 } 136 } 137 } 138 139 } 140 void SoundMove() 141 { 142 if (photonView.IsMine) 143 { 144 AudioSource.PlayClipAtPoint(MoveSound, transform.position); 145 } 146 } 147 148 [PunRPC] 149 private void Move1() 150 { 151 transform.rotation = Quaternion.AngleAxis(90, new Vector3(0, 1, 0)); 152 transform.position += transform.forward * speed * Time.deltaTime; 153 } 154 [PunRPC] 155 private void Move2() 156 { 157 transform.rotation = Quaternion.AngleAxis(-90, new Vector3(0, 1, 0)); 158 transform.position += transform.forward * speed * Time.deltaTime; 159 } 160 [PunRPC] 161 private void Move3() 162 { 163 transform.rotation = Quaternion.AngleAxis(45, new Vector3(0, 1, 0)); 164 transform.position += transform.forward * speed * Time.deltaTime; 165 } 166 [PunRPC] 167 private void Move4() 168 { 169 transform.rotation = Quaternion.AngleAxis(-45, new Vector3(0, 1, 0)); 170 transform.position += transform.forward * speed * Time.deltaTime; 171 } 172 [PunRPC] 173 private void Move5() 174 { 175 transform.rotation = Quaternion.AngleAxis(0, new Vector3(0, 1, 0)); 176 transform.position += transform.forward * speed * Time.deltaTime; 177 } 178 [PunRPC] 179 private void Move6() 180 { 181 transform.rotation = Quaternion.AngleAxis(135, new Vector3(0, 1, 0)); 182 transform.position += transform.forward * speed * Time.deltaTime; 183 } 184 [PunRPC] 185 private void Move7() 186 { 187 transform.rotation = Quaternion.AngleAxis(-135, new Vector3(0, 1, 0)); 188 transform.position += transform.forward * speed * Time.deltaTime; 189 } 190 [PunRPC] 191 private void Move8() 192 { 193 transform.rotation = Quaternion.AngleAxis(180, new Vector3(0, 1, 0)); 194 transform.position += transform.forward * speed * Time.deltaTime; 195 } 196 197} 198

変更後の移動スクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6public class move_joystick2 : MonoBehaviourPun 7{ 8 9 //public FixedJoystick joystick; 10 public FloatingJoystick joystick2; 11 PlayerID script; 12 13 private float speed = 5.0f; 14 private float jumpPower = 275; 15 private float delay = 0f; 16 public AudioClip MoveSound; 17 public AudioClip JumpSound; 18 private Rigidbody rb; 19 20 private float radian; 21 22 // Start is called before the first frame update 23 void Start() 24 { 25 26 if (photonView.IsMine) 27 { 28 rb = GetComponent<Rigidbody>(); 29 } 30 31 GameObject UI = GameObject.Find("BattleUI"); 32 script = UI.GetComponent<PlayerID>(); 33 joystick2 = script.joystick2; 34 35 } 36 37 38 // Update is called once per frame 39 void Update() 40 { 41 var x = joystick2.Horizontal; 42 var y = joystick2.Vertical; 43 radian = Mathf.Atan2(x, y) * Mathf.Rad2Deg; 44 45 if (radian < 0) 46 { 47 radian += 360; 48 } 49 50 if (photonView.IsMine) 51 { 52 Ray ray = new Ray(transform.position, -transform.up); 53 { 54 //接地判定 55 RaycastHit hit; 56 if (Physics.Raycast(ray, out hit, 0.25f)) 57 { 58 if (Input.GetKeyDown(KeyCode.O)) 59 { 60 rb.AddForce(transform.up * jumpPower); 61 AudioSource.PlayClipAtPoint(JumpSound, transform.position); 62 } 63 64 } 65 } 66 67 if (x == 0) 68 { 69 if (y == 0) 70 { 71 return; 72 } 73 } 74 75 photonView.RPC(nameof(Move), RpcTarget.All); 76 Invoke("SoundMove", delay); 77 78 } 79 80 } 81 void SoundMove() 82 { 83 if (photonView.IsMine) 84 { 85 AudioSource.PlayClipAtPoint(MoveSound, transform.position); 86 } 87 } 88 89 [PunRPC] 90 private void Move() 91 { 92 transform.rotation = Quaternion.AngleAxis(radian, new Vector3(0, 1, 0)); 93 transform.position += transform.forward * speed * Time.deltaTime; 94 } 95 96} 97

補足情報(FW/ツールのバージョンなど)

Unity 2019.4.11f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問