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

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

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

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

Unity

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

Q&A

0回答

1334閲覧

AddTorque();関数で力を消す方法

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

Unity

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

0グッド

0クリップ

投稿2021/04/09 04:11

提示コードの回転のコメント部のコードですが十字キーの上下を押したときに力をかけて上下に回転するのですが。キーを離した時にその力を消方法はあるのでしょうか?調べましたがわかりません。

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerController : MonoBehaviour 6{ 7 [SerializeField] float wingsArea; //翼の面積 8 [SerializeField] float airDensity; //空気密度 9 [SerializeField] float speed; //速度 10 [SerializeField] float cl; //揚力係数 11 12 13 Rigidbody rb; 14 15 float L; //揚力 16 Vector3 moveVec; //向き 17 Vector3 angleVec; //回転 18 19 20 // Start is called before the first frame update 21 void Start() 22 { 23 rb = GetComponent<Rigidbody>(); 24 moveVec = new Vector3(0,0,0); 25 angleVec = new Vector3(0,0,0); 26 27 L = 0; 28 29 } 30 31 // Update is called once per frame 32 void Update() 33 { 34 if(Input.GetKey(KeyCode.W)) 35 { 36 37 } 38 else if (Input.GetKey(KeyCode.S)) 39 { 40 } 41 else 42 { 43 44 } 45 46 47 48 if(Input.GetKey(KeyCode.UpArrow)) 49 { 50 angleVec.x = 1f; 51 cl += -1; 52 53 } 54 else if (Input.GetKey(KeyCode.DownArrow)) 55 { 56 angleVec.x = -1f; 57 cl += 1; 58 } 59 else 60 { 61 angleVec.y = 0; 62 } 63 64 65 //ヨール 66 if (Input.GetKey(KeyCode.LeftArrow)) 67 { 68 angleVec.z = 40; 69 70 } 71 else if (Input.GetKey(KeyCode.RightArrow)) 72 { 73 angleVec.z = -40; 74 75 } 76 else 77 { 78 angleVec.z = 0; 79 } 80 81 82 83 84 //揚力計算 85// moveVec.y = lift(airDensity, wingsArea, speed, cl) / 1000 * Time.deltaTime; //揚力 86 87 88// Debug.Log(moveVec.y); 89 90// moveVec.z = speed; 91 } 92 93 float m_AeroFactor = 0; 94 95 96 void FixedUpdate() 97 { 98 99 //前進 100 var forces = Vector3.zero; 101 forces = speed * transform.forward; 102 Vector3 liftDirection = Vector3.Cross(rb.velocity, transform.right).normalized; 103 var liftPower = lift(airDensity, wingsArea, speed, cl) / 1000 * Time.deltaTime; 104 forces += liftPower * liftDirection; 105 rb.AddForce(forces); 106 107 //回転 108 var torque = Vector3.zero; 109 torque += angleVec.x * transform.right; 110 torque += angleVec.z * transform.forward; 111 rb.AddTorque(torque * speed * Time.deltaTime); 112 113 114 115 116 117 // "Aerodynamic" calculations. This is a very simple approximation of the effect that a plane 118 // will naturally try to align itself in the direction that it's facing when moving at speed. 119 // Without this, the plane would behave a bit like the asteroids spaceship! 120 if (rb.velocity.magnitude > 0) 121 { 122 // compare the direction we're pointing with the direction we're moving: 123 m_AeroFactor = Vector3.Dot(transform.forward,rb.velocity.normalized); 124 // multipled by itself results in a desirable rolloff curve of the effect 125 m_AeroFactor *= m_AeroFactor; 126 // Finally we calculate a new velocity by bending the current velocity direction towards 127 // the the direction the plane is facing, by an amount based on this aeroFactor 128 var newVelocity = Vector3.Lerp(rb.velocity, transform.forward * speed, 129 m_AeroFactor * speed * 1f * Time.deltaTime); 130 rb.velocity = newVelocity; 131 132 // also rotate the plane towards the direction of movement - this should be a very small effect, but means the plane ends up 133 // pointing downwards in a stall 134 rb.rotation = Quaternion.Slerp(rb.rotation, 135 Quaternion.LookRotation(rb.velocity, transform.up), 136 1f * Time.deltaTime); 137 } 138 139 140 141 } 142 143 144 145 146 147 //揚力を取得 148 float lift(float air,float wing,float spd,float cl) 149 { 150 float l = (air / 2) * wing * spd * spd * cl; //揚力計算 151 152 153 return l; 154 } 155 156 157 158 159} 160

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問