パズルゲームを作っているのですが、今現在、パーツにRigitbodyを加え、positionの固定を使用としています。
クリックしているオブジェクトのみ動くようにしたいのですが常時固定されてしまい、動かなくなりました。
クリックしているオブジェクトのみを動かす方法としてどこを変更したらいいとかありますか?
C#
1コード 2 3const float MaxDistance = 0.45f; 4 Rigidbody rigidbody; 5 private Vector3 screenPoint; 6 private Vector3 offset; 7 8 class Edge 9 { 10 public Transform transform; 11 public Transform pair; 12 public float PairCount = 7; 13 14 15 16 public bool Check() 17 { 18 float distance = Vector3.Distance(transform.position, pair.position); 19 if (distance > MaxDistance) 20 return false; 21 22 float rotationDelta = transform.eulerAngles.y - pair.eulerAngles.y; 23 rotationDelta = Mathf.Abs(rotationDelta); 24 if (rotationDelta > 10) 25 return false; 26 27 return true; 28 } 29 30 public Vector3 GetDistance() 31 { 32 return pair.position - transform.position; 33 } 34 } 35 36 List<Edge> edges = new List<Edge>(); 37 38 // Start is called before the first frame update 39 void Start() 40 { 41 rigidbody = GetComponent<Rigidbody>(); 42 rigidbody.constraints = RigidbodyConstraints.FreezePosition;//移動しない 43 rigidbody.constraints = RigidbodyConstraints.FreezeRotation;//回転しない 44 foreach (Transform child in transform) 45 { 46 Edge edge = new Edge(); 47 edge.transform = child; 48 49 string otherName; 50 char lastCharacter = child.name[child.name.Length - 1]; 51 if (lastCharacter == 'A') 52 { 53 otherName = child.name.Substring(0, child.name.Length - 1) + "B"; 54 } 55 else 56 { 57 otherName = child.name.Substring(0, child.name.Length - 1) + "A"; 58 } 59 60 edge.pair = GameObject.Find(otherName).transform; 61 62 edges.Add(edge); 63 } 64 65 66 } 67 public void onClickAct() 68 { 69if (Input.GetMouseButtonUp(0)) 70 { 71 foreach(Edge edge in edges) 72 { 73 if (edge.Check()) 74 { 75 transform.position += edge.GetDistance(); 76 } 77 } 78 } 79 }
・「onClickAct」が正しく呼ばれているか確認してください。(このコード内からは呼ばれていません)
・「edge.Check()」が想定通りの動きをしていることを確認してください。
・GetMouseButtonUpは「マウスのボタンが離された直後の1フレーム」しかtrueになりません。動く部分が「transform.position += edge.GetDistance();」だとすると、動くとしてもクリック後の1フレームのみです。多分希望の動きと違うので「unity マウス 押している間」などでググってみてください。
input.GetMouseButton() で、常にマウスの状態を確認し、処理する。
でしょうか?
あなたの回答
tips
プレビュー