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

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

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

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

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

Unity

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

Q&A

0回答

785閲覧

unity. テニスでラリーをしたい!

Happy_Kid

総合スコア4

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2021/07/26 12:47

発生している問題・エラーメッセージ

テニスでラリーができるロボットを作ろうとしてますが.打ち返しもしないし高度が24.7で固定されてしまいます

該当のソースコード

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class rally : MonoBehaviour 6{ 7 public GameObject Ball; 8 public ball script; 9 public player playerpro; 10 public float speedZ = 1000f; //スピード 11 public float speedY = 8000f; 12 Vector3 sliderDirection = new Vector3(0f, 1f, -1f); 13 int changePower = 700; 14 Vector3 sliderDirection2 = new Vector3(0f, 0f, -1.5f); 15 Vector3 sliderDirection3 = new Vector3(0f, 0f, -0.12625f); 16 Vector3 sliderDirection4 = new Vector3(0.4f, 0f, 0.3f); 17 Vector3 sliderDirection5 = new Vector3(0f, 0f, -0.3f); 18 Vector3 sliderDirection6 = new Vector3(0.05f, 0f, -1.6f); 19 Vector3 sliderDirection7 = new Vector3(0.02f, 0f, 2f); 20 Vector3 sliderDirection8 = new Vector3(0f, -1f, 3f); 21 22 // Start is called before the first frame update 23 void Start() 24 { 25 StartCoroutine("move"); 26 } 27 28 // Update is called once per frame 29 void Update() 30 { 31 if(playerpro.shoot == true) 32 { 33 script.enter = false; 34 } 35 36 } 37 38 IEnumerator move() 39 { 40 while(script.enter == true) 41 { 42 transform.position = new Vector3(script.sphere.transform.position.x, 0f, 0f); 43 44 45 yield return null; 46 } 47 StartCoroutine("moveII"); 48 49 50 51 52 53 } 54 IEnumerator moveII() 55 { 56 while (script.enter == false) 57 { 58 transform.position = new Vector3(script.sphere.transform.position.x, script.sphere.transform.position.y, script.transform.position.z); 59 60 61 yield return null; 62 } 63 StartCoroutine("moveI"); 64 65 66 67 68 69 } 70 private void OnCollisionEnter(Collision col) 71 { 72 playerpro.shoot = true; 73 if (col.gameObject.tag == "Ball") 74 { 75 Vector3 Force; 76 Force = new Vector3(0, speedY * -1f, speedZ * -1f); 77 Ball.GetComponent<Rigidbody>().AddForce(Force * -1f, ForceMode.Impulse); 78 79 80 81 if (Input.GetKey(KeyCode.N)) 82 { 83 84 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection2 * changePower);//ボールに力を加え 85 } 86 if (Input.GetKey(KeyCode.B)) 87 { 88 89 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection3 * changePower);//ボールに力を加え 90 } 91 if (Input.GetKey(KeyCode.V)) 92 { 93 94 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection4 * changePower);//ボールに力を加え 95 } 96 if (Input.GetKey(KeyCode.C)) 97 { 98 99 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection6 * changePower);//ボールに力を加え 100 } 101 if (Input.GetKey(KeyCode.X)) 102 { 103 104 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection7 * changePower);//ボールに力を加え 105 } 106 if (Input.GetKey(KeyCode.Z)) 107 { 108 109 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection8 * changePower);//ボールに力を加え 110 } 111 } 112 } 113} 114

c#

1//上のプログラムのballに該当する部分です.ちなみにボールの高度は24.7ではないです 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class ball : MonoBehaviour 7{ Rigidbody rb; 8 public bool enter; 9 public Transform sphere; 10 // Start is called before the first frame update 11 void Start() 12 { 13 rb = GetComponent<Rigidbody>(); 14 enter = true; 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 21 } 22 void OnTriggerEnter(Collider col) 23 { 24 25 if(col.gameObject.tag == "drag") 26 { 27 enter = true; 28 Debug.Log("in"); 29 Vector3 sliderDirection = new Vector3(0f, -0.8f, 0f); 30 int changePower = 10; //変化させる力 31 rb.AddForce(sliderDirection * changePower);//ボールに力を加える 32 } 33 34 } 35 private void OnTriggerExit(Collider col) 36 { 37 if (col.gameObject.tag == "drag") 38 { 39 40 Vector3 sliderDirection = new Vector3(0f, -0.1f, 0f); 41 int changePower = 100; //変化させる力 42 rb.AddForce(sliderDirection * changePower);//ボールに力を加える 43 } 44 45 } 46 private void OnCollisionEnter(Collision col) 47 { 48 if(col.gameObject.tag == "in") 49 { 50 enter = false; 51 } 52 } 53} 54 55```c# 56 57```c# 58//一番上のスクリプトのplayerproの部分です 59using System.Collections; 60using System.Collections.Generic; 61using UnityEngine; 62 63public class player : MonoBehaviour 64{ 65 public GameObject Ball; 66 public bool shoot;//ボール 67 public float speedZ = 1000f; //スピード 68 public float speedY = 8000f; 69 Vector3 sliderDirection = new Vector3(0f, 1f, -1f); 70 int changePower = 700; 71 Vector3 sliderDirection2 = new Vector3(0f, 1.25f, -1.5f); 72 Vector3 sliderDirection3 = new Vector3(0f, 1.55f, -0.12625f); 73 Vector3 sliderDirection4 = new Vector3(0.4f, 0.5f, 0.3f); 74 Vector3 sliderDirection5 = new Vector3(0f, 1.4f, -0.3f); 75 Vector3 sliderDirection6 = new Vector3(0.05f, 0f, -1.6f); 76 Vector3 sliderDirection7 = new Vector3(0.02f, 0f, 2f); 77 Vector3 sliderDirection8 = new Vector3(0f, -1f, 3f); 78 79 void Update() 80 { 81 if (Input.GetKey(KeyCode.W)) 82 { 83 transform.position += transform.forward * -0.15f; 84 85 } 86 if (Input.GetKey(KeyCode.S)) 87 { 88 transform.position += transform.forward * 0.15f; 89 90 } 91 if (Input.GetKey(KeyCode.A)) 92 { 93 transform.position += transform.right * 0.15f; 94 95 } 96 if (Input.GetKey(KeyCode.D)) 97 { 98 transform.position += transform.right * -0.15f; 99 100 } 101 102 if (Input.GetKey(KeyCode.Q)) 103 { 104 transform.Rotate(0, -10, 0); 105 } 106 if (Input.GetKey(KeyCode.E)) 107 { 108 transform.Rotate(0, 10, 0); 109 } 110 } 111 private void OnCollisionEnter(Collision col) 112 { 113 shoot = true; 114 if (col.gameObject.tag == "Ball") 115 { 116 Vector3 Force; 117 Force = new Vector3(0, speedY*-1f, speedZ*-1f); 118 Ball.GetComponent<Rigidbody>().AddForce(Force*-1f, ForceMode.Impulse); 119 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection5 * changePower);//ボールに力を加え 120 121 if (Input.GetKey(KeyCode.M)) 122 { 123 124 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection * changePower);//ボールに力を加え 125 } 126 if (Input.GetKey(KeyCode.N)) 127 { 128 129 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection2 * changePower);//ボールに力を加え 130 } 131 if (Input.GetKey(KeyCode.B)) 132 { 133 134 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection3 * changePower);//ボールに力を加え 135 } 136 if (Input.GetKey(KeyCode.V)) 137 { 138 139 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection4 * changePower);//ボールに力を加え 140 } 141 if (Input.GetKey(KeyCode.C)) 142 { 143 144 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection6 * changePower);//ボールに力を加え 145 } 146 if (Input.GetKey(KeyCode.X)) 147 { 148 149 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection7 * changePower);//ボールに力を加え 150 } 151 if (Input.GetKey(KeyCode.Z)) 152 { 153 154 Ball.GetComponent<Rigidbody>().AddForce(sliderDirection8 * changePower);//ボールに力を加え 155 } 156 } 157 } 158} 159 160
### 試したこと 手動操作で移動確認をしました.おそらく1個目のプログラムのIEnumerator move()とIEnumerator moveII() が理由だと思いますが理由はわかりません. ### 補足情報(FW/ツールのバージョンなど) unityのバージョンは2020.1.3f1です

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問