実現したいこと
ゲーム開始時に生成されるキャラクターをジョイスティックで操作したい
発生している問題
キャラクターのprefabにジョイスティックをアタッチできない
(ジョイスティックのprefabならアタッチできるが,そこからどのようにすればよいか分からない)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6public class move_joystick : MonoBehaviourPun 7{ 8public FixedJoystick joystick;//FixedJoystickを取得 9 10private float speed = 5.0f; 11private float jumpPower = 275; 12private float delay = 0f; 13public AudioClip MoveSound; 14public AudioClip JumpSound; 15private Rigidbody rb; 16// Start is called before the first frame update 17void Start() 18{ 19if (photonView.IsMine) 20{ 21rb = GetComponent<Rigidbody>(); 22} 23} 24 25 26// Update is called once per frame 27void Update() 28{ 29var x = joystick.Horizontal; 30var y = joystick.Vertical; 31float radian = Mathf.Atan2(x, y) * Mathf.Rad2Deg; 32 33if (radian < 0) 34{ 35radian += 360; 36} 37Debug.Log(radian); 38 39if(photonView.IsMine) 40{ 41if (x == 0) 42{ 43if (y == 0) 44{ 45return; 46} 47} 48 49if (radian <= 15) 50{ 51 52photonView.RPC(nameof(Move5), RpcTarget.All); 53Invoke("SoundMove", delay); 54} 55 56else if (radian <= 75) 57{ 58 59photonView.RPC(nameof(Move3), RpcTarget.All); 60Invoke("SoundMove", delay); 61} 62 63else if (radian <= 105) 64{ 65 66photonView.RPC(nameof(Move1), RpcTarget.All); 67Invoke("SoundMove", delay); 68} 69 70else if (radian <= 165) 71{ 72 73photonView.RPC(nameof(Move6), RpcTarget.All); 74Invoke("SoundMove", delay); 75} 76 77else if (radian <= 195) 78{ 79 80photonView.RPC(nameof(Move8), RpcTarget.All); 81Invoke("SoundMove", delay); 82} 83 84else if (radian <= 255) 85{ 86 87photonView.RPC(nameof(Move7), RpcTarget.All); 88Invoke("SoundMove", delay); 89} 90 91else if (radian <= 285) 92{ 93 94photonView.RPC(nameof(Move2), RpcTarget.All); 95Invoke("SoundMove", delay); 96} 97 98else if (radian <= 345) 99{ 100 101photonView.RPC(nameof(Move4), RpcTarget.All); 102Invoke("SoundMove", delay); 103} 104 105else if (radian <= 360) 106{ 107 108photonView.RPC(nameof(Move5), RpcTarget.All); 109Invoke("SoundMove", delay); 110} 111} 112 113Ray ray = new Ray(transform.position, -transform.up); 114{ 115//接地判定 116RaycastHit hit; 117if (Physics.Raycast(ray, out hit, 0.25f)) 118{ 119if (Input.GetKeyDown(KeyCode.O)) 120{ 121rb.AddForce(transform.up * jumpPower); 122AudioSource.PlayClipAtPoint(JumpSound, transform.position); 123} 124 125} 126} 127 128} 129void SoundMove() 130{ 131if (photonView.IsMine) 132{ 133AudioSource.PlayClipAtPoint(MoveSound, transform.position); 134} 135} 136 137[PunRPC] 138private void Move1() 139{ 140transform.rotation = Quaternion.AngleAxis(90, new Vector3(0, 1, 0)); 141transform.position += transform.forward * speed * Time.deltaTime; 142} 143[PunRPC] 144private void Move2() 145{ 146transform.rotation = Quaternion.AngleAxis(-90, new Vector3(0, 1, 0)); 147transform.position += transform.forward * speed * Time.deltaTime; 148} 149[PunRPC] 150private void Move3() 151{ 152transform.rotation = Quaternion.AngleAxis(45, new Vector3(0, 1, 0)); 153transform.position += transform.forward * speed * Time.deltaTime; 154} 155[PunRPC] 156private void Move4() 157{ 158transform.rotation = Quaternion.AngleAxis(-45, new Vector3(0, 1, 0)); 159transform.position += transform.forward * speed * Time.deltaTime; 160} 161[PunRPC] 162private void Move5() 163{ 164transform.rotation = Quaternion.AngleAxis(0, new Vector3(0, 1, 0)); 165transform.position += transform.forward * speed * Time.deltaTime; 166} 167[PunRPC] 168private void Move6() 169{ 170transform.rotation = Quaternion.AngleAxis(135, new Vector3(0, 1, 0)); 171transform.position += transform.forward * speed * Time.deltaTime; 172} 173[PunRPC] 174private void Move7() 175{ 176transform.rotation = Quaternion.AngleAxis(-135, new Vector3(0, 1, 0)); 177transform.position += transform.forward * speed * Time.deltaTime; 178} 179[PunRPC] 180private void Move8() 181{ 182transform.rotation = Quaternion.AngleAxis(180, new Vector3(0, 1, 0)); 183transform.position += transform.forward * speed * Time.deltaTime; 184} 185 186}
試したこと
もともとHierarchyに置いてあったジョイスティックをプレハブにアタッチしようとしてもできなかった
補足情報(FW/ツールのバージョンなど)
Unity 2019.4.11f1
ジョイスティックはAsset StoreのJoystick Pack(無料)を使用させていただいております
回答2件
あなたの回答
tips
プレビュー