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

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

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

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

Unity3D

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

Unity

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

Q&A

解決済

1回答

1882閲覧

Unity 余韻をつけてゆっくり止める

sanshi5

総合スコア18

C#

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

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2021/05/16 22:58

編集2021/05/17 05:17

前提・実現したいこと

マウスで方向をきめて
Wキーで進むコントローラー(車を動かすなどの目的)をつくり
Wキーを離したとき余韻で少し前に進むかんじにする。

該当のソースコード

Playerのスクリプトは動かしたいPlayerにCameraのスクリプトはカメラに付けてます。
Playerのコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerCont : MonoBehaviour 6{ 7 8 [SerializeField] private Vector3 velocity; 9 [SerializeField] private float moveSpeed = 5.0f; 10 [SerializeField] private float applySpeed = 0.2f; 11 [SerializeField] private CamCont refCamera; 12 13 [SerializeField] private GameObject _Obj; 14 [SerializeField] private float _TravelTime = 1.0f; 15 [SerializeField] private float _Length = -1.0f; 16 17 void Update() 18 { 19 velocity = Vector3.zero; 20 // Wキー押してる間進む 21 if (Input.GetKey(KeyCode.W)) 22 velocity.z += 1; 23 24 velocity = velocity.normalized * moveSpeed * Time.deltaTime; 25 26 if (velocity.magnitude > 0) 27 { 28 transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(refCamera.hRotation * -velocity),applySpeed); 29 transform.position += refCamera.hRotation * velocity; 30 } 31 32 // Wキー放して余韻進む 33 if (Input.GetKeyUp(KeyCode.W)) 34 { 35 Vector3 _nowPos = _Obj.transform.position; 36 Vector3 _targetPos = new Vector3(_nowPos.x + _Length, _nowPos.y, _nowPos.z); 37 38 StartCoroutine(AnimateCoroutine(_Obj.transform, _TravelTime, _targetPos, null)); 39 40 } 41 42 IEnumerator AnimateCoroutine(Transform transform, float time, Vector3? position, Quaternion? rotation) 43 { 44 var currentPosition = transform.position; 45 var currentRotation = transform.rotation; 46 47 var targetPosition = position ?? currentPosition; 48 var targetRotation = rotation ?? currentRotation; 49 50 var sumTime = 0f; 51 while (true) 52 { 53 sumTime += Time.deltaTime; 54 var ratio = sumTime / time; 55 56 transform.SetPositionAndRotation( 57 Vector3.Lerp(currentPosition, targetPosition, ratio), 58 Quaternion.Lerp(currentRotation, targetRotation, ratio) 59 ); 60 61 if (ratio > 1.0f) 62 { 63 break; 64 } 65 66 yield return null; 67 } 68 } 69 } 70}

Camera側のコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class CamCont : MonoBehaviour 6 7{ 8 [SerializeField] private float turnSpeed = 10.0f; 9 [SerializeField] private Transform player; 10 [SerializeField] private float distance = 15.0f; 11 [SerializeField] private Quaternion vRotation; 12 [SerializeField] public Quaternion hRotation; 13 14 void Start() 15 { 16 vRotation = Quaternion.Euler(30, 0, 0); 17 hRotation = Quaternion.identity; 18 transform.rotation = hRotation * vRotation; 19 transform.position = player.position - transform.rotation * Vector3.forward * distance; 20 } 21 22 void LateUpdate() 23 { 24 if (Input.GetMouseButton(0)) 25 hRotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * turnSpeed, 0); 26 transform.rotation = hRotation * vRotation; 27 transform.position = player.position + new Vector3(0, 3, 0) - transform.rotation * Vector3.forward * distance; 28 } 29}

試したこと

公開されてるスクリプトを組み合わせ加工して
マウスでカメラ回転に合わせて前進して
キーUp時に指定の距離だけ移動できるコードを
組み合わせてみた。

うまくいっていないところ

進行方向に余韻がいかず一方向にしかすべってくれない。 
補足動画 https://youtu.be/43bmJk4-mRQ

知りたいこと

進行方向に余韻を持たせるにはどうすればいいか。

補足情報(FW/ツールのバージョンなど)

Unity2019.4.9f1

進捗修正追記

Vector3 _nowPos = _Obj.transform.position;のところを書き換えました。
調べてtransform.forwardがオブジェクトの向きを取得するらしかったので
試行錯誤したらなんとなく目的の動きをしてくれました。
このような書き方でいいのかなどまだ検証中ですが、動作チェックをしてます。
キーを離したタイミングわかりずらいですが検証動画もアップします。
https://youtu.be/c4UIkRzcsbk

C#

1 Vector3 _nowPos = _Obj.transform.position -_Obj.transform.forward * _Length; 2 Vector3 _targetPos = new Vector3(_nowPos.x , _nowPos.y, _nowPos.z);

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

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

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

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

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

guest

回答1

0

自己解決

transform.forwardを使うことで進行方向に止まりました。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerCont : MonoBehaviour 6{ 7 8 [SerializeField] private Vector3 velocity; 9 [SerializeField] private float moveSpeed = 5.0f; 10 [SerializeField] private float applySpeed = 0.2f; 11 [SerializeField] private CamCont refCamera; 12 13 [SerializeField] private GameObject _Obj; 14 [SerializeField] private float _TravelTime = 1.0f; 15 [SerializeField] private float _Length = 2.0f; 16 17 void Update() 18 { 19 velocity = Vector3.zero; 20 // Wキー押してる間進む 21 if (Input.GetKey(KeyCode.W)) 22 velocity.z += 1; 23 24 velocity = velocity.normalized * moveSpeed * Time.deltaTime; 25 26 if (velocity.magnitude > 0) 27 { 28 transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(refCamera.hRotation * -velocity),applySpeed); 29 transform.position += refCamera.hRotation * velocity; 30 } 31 32 // Wキー放して余韻進む 33 if (Input.GetKeyUp(KeyCode.W)) 34 { 35 36 Vector3 _nowPos = _Obj.transform.position -_Obj.transform.forward * _Length; 37 Vector3 _targetPos = new Vector3(_nowPos.x , _nowPos.y, _nowPos.z); 38 39 StartCoroutine(AnimateCoroutine(_Obj.transform, _TravelTime, _targetPos, null)); 40 41 42 } 43 44 IEnumerator AnimateCoroutine(Transform transform, float time, Vector3? position, Quaternion? rotation) 45 { 46 var currentPosition = transform.position; 47 var currentRotation = transform.rotation; 48 49 var targetPosition = position ?? currentPosition; 50 var targetRotation = rotation ?? currentRotation; 51 52 var sumTime = 0f; 53 while (true) 54 { 55 sumTime += Time.deltaTime; 56 var ratio = sumTime / time; 57 58 transform.SetPositionAndRotation( 59 Vector3.Lerp(currentPosition, targetPosition, ratio), 60 Quaternion.Lerp(currentRotation, targetRotation, ratio) 61 ); 62 63 if (ratio > 1.0f) 64 { 65 break; 66 } 67 68 yield return null; 69 } 70 71 } 72 } 73}

投稿2021/05/17 06:46

sanshi5

総合スコア18

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問