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

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

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

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

Unity3D

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

Unity

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

Q&A

解決済

2回答

26760閲覧

Unityで回転制限をつけたいです。

Reonashachou

総合スコア9

C#

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

Unity3D

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

Unity

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

0グッド

1クリップ

投稿2017/07/08 02:44

###前提・実現したいこと

下記コードでAキー、Dキーを押している間それぞれY軸の-方向、Y軸の+方向に回転するよう書きましたが、90度回転したら回転できないようにする機能をつけたいです。どのようにすればいいでしょうか?下記のコードに書き足す形で回答していただけると幸いです。わかりにくい質問文かと思いますがよろしくおねがいいたします。

###該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class objectMoving : MonoBehaviour { 6 public float wSpeed = 10; 7 public float sSpeed = 7; 8 public float adRotate = 100; 9 10 // Use this for initialization 11 void Start () { 12 13 } 14 15 // Update is called once per frame 16 void Update () { 17 if (Input.GetKey (KeyCode.W)) { 18 this.transform.position += new Vector3 (0, 0, wSpeed) * Time.deltaTime; 19 } 20 if (Input.GetKey (KeyCode.S)) { 21 this.transform.position += new Vector3 (0, 0, -sSpeed) * Time.deltaTime; 22 } 23 if (Input.GetKey (KeyCode.D)) { 24 this.transform.Rotate(new Vector3 (0, adRotate, 0) * Time.deltaTime); 25 } 26 if (Input.GetKey (KeyCode.A)) { 27 this.transform.Rotate(new Vector3 (0, -adRotate, 0) * Time.deltaTime); 28 } 29 } 30} 31

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

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

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

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

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

KSwordOfHaste

2017/07/08 09:54

「前提・実現したいこと」はコードではないので、コードタグ(```)で囲まないでください。
guest

回答2

0

transform.rotateは「現在の状態からさらに回転」という操作なので、「今いったい何度回転しているのか?」ということを意識しにくい方法です。回転角度の制限をつけたい場合は、自分で計算して、「初期状態から何度回転せよ」というふうな指定方法を用いるとよいと思います。

pikumin001さんの回答のように、transform.rotationへ代入する方法が一般性が高いと思いますが、特定の軸の周りに単純に回転したいならeulerAnglesに角度を直接指定してもよいと思います。自分の回答はeulerAngleを用いた例です。

またMathf.Clampという関数があり、値をある範囲内に収めるような計算をしたい場合には活用すると便利だと思います。

C#

1using UnityEngine; 2 3public class objectMoving : MonoBehaviour { 4 public float wSpeed = 10; 5 public float sSpeed = 7; 6 public float adRotate = 100; 7 float yRotate = 0; 8 9 // Use this for initialization 10 void Start () { 11 12 } 13 14 // Update is called once per frame 15 void Update () { 16 if (Input.GetKey (KeyCode.W)) { 17 this.transform.position += new Vector3 (0, 0, wSpeed) * Time.deltaTime; 18 } 19 if (Input.GetKey (KeyCode.S)) { 20 this.transform.position += new Vector3 (0, 0, -sSpeed) * Time.deltaTime; 21 } 22 if (Input.GetKey (KeyCode.D)) { 23 yRotate = Mathf.Clamp(yRotate + adRotate * Time.deltaTime, -90, 90); 24 transform.eulerAngles = new Vector3(0, yRotate, 0); 25 } 26 if (Input.GetKey (KeyCode.A)) { 27 yRotate = Mathf.Clamp(yRotate - adRotate * Time.deltaTime, -90, 90); 28 transform.eulerAngles = new Vector3(0, yRotate, 0); 29 } 30 } 31}

投稿2017/07/08 10:54

KSwordOfHaste

総合スコア18392

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

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

0

ベストアンサー

これで一応ご質問どおりのことはできます。
tmpRotateに最初の状態から何度うごいたかを入れて、90度を超えるようだとそれ以上動かないよう、
if分で条件分岐してあります。
startRotationは、たとえばグローバル座標の回転がY 30だったとした場合、右に90度回転すると120度になりますよね。
でも、adRotate * Time.deltaTimeを加算していくと120度を超えてしまう場合があるので、
初期回転座標+90度に強制的に設定するように、
this.transform.rotation = Quaternion.Euler(0, startRotation+90, 0);
としてあります。

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class objectMoving : MonoBehaviour { 6public float wSpeed = 10; 7 public float sSpeed = 7; 8 public float adRotate = 100; 9 10 11 float maxRotate = 90;//回転角の最大値// 12 float tmpRotate = 0;//現在の回転角// 13 float startRotation;//最初のグローバルY座標// 14 // Use this for initialization 15 void Start() 16 { 17 startRotation = this.transform.rotation.eulerAngles.y; //最初のグローバルのY座標を代入// 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 if (Input.GetKey(KeyCode.W)) 24 { 25 this.transform.position += new Vector3(0, 0, wSpeed) * Time.deltaTime; 26 } 27 if (Input.GetKey(KeyCode.S)) 28 { 29 this.transform.position += new Vector3(0, 0, -sSpeed) * Time.deltaTime; 30 } 31 if (Input.GetKey(KeyCode.D)) 32 { 33 this.transform.Rotate(new Vector3(0, adRotate, 0) * Time.deltaTime); 34 tmpRotate += (adRotate * Time.deltaTime); 35 if (tmpRotate >= maxRotate) 36 { 37 this.transform.rotation = Quaternion.Euler(0, startRotation+90, 0); 38 tmpRotate = 90; 39 } 40 } 41 if (Input.GetKey(KeyCode.A)) 42 { 43 this.transform.Rotate(new Vector3(0, -adRotate, 0) * Time.deltaTime); 44 tmpRotate -= (adRotate * Time.deltaTime); 45 if (tmpRotate <= maxRotate * -1) 46 { 47 this.transform.rotation = Quaternion.Euler(0, startRotation - 90, 0); 48 tmpRotate = -90; 49 } 50 } 51 } 52}

投稿2017/07/08 10:31

pikumin001

総合スコア132

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問