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

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

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

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

Unity3D

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

Unity

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

Q&A

解決済

1回答

1900閲覧

Unity Standard Aseet の Free Look CameraRig の視点の回転方法変更したい

Y0241-N

総合スコア1066

C#

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

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2019/05/24 07:20

編集2019/05/24 08:33

実現したいこと

Unity Standard Aseet にある Cameras の Free Look CameraRig を使用しているのですが、
デフォルトではシーン再生時、視点の回転が常にマウスの動きと連動しているのを、

左クリックを押しながらマウスを動かした時のみ、視点が回転するように変更を加えたいのですが、
スクリプトのどの部分を変更すれば良いのかが分からず、変更部分を教えていただきたく思います。

Free Look Camera のスクリプト

using System; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; namespace UnityStandardAssets.Cameras { public class FreeLookCam : PivotBasedCameraRig { [SerializeField] private float m_MoveSpeed = 1f; // How fast the rig will move to keep up with the target's position. [Range(0f, 10f)] [SerializeField] private float m_TurnSpeed = 1.5f; // How fast the rig will rotate from user input. [SerializeField] private float m_TurnSmoothing = 0.0f; // How much smoothing to apply to the turn input, to reduce mouse-turn jerkiness [SerializeField] private float m_TiltMax = 75f; // The maximum value of the x axis rotation of the pivot. [SerializeField] private float m_TiltMin = 45f; // The minimum value of the x axis rotation of the pivot. [SerializeField] private bool m_LockCursor = false; // Whether the cursor should be hidden and locked. [SerializeField] private bool m_VerticalAutoReturn = false; // set wether or not the vertical axis should auto return private float m_LookAngle; private float m_TiltAngle; private const float k_LookDistance = 100f; private Vector3 m_PivotEulers; private Quaternion m_PivotTargetRot; private Quaternion m_TransformTargetRot; protected override void Awake() { base.Awake(); Cursor.lockState = m_LockCursor ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !m_LockCursor; m_PivotEulers = m_Pivot.rotation.eulerAngles; m_PivotTargetRot = m_Pivot.transform.localRotation; m_TransformTargetRot = transform.localRotation; } protected void Update() { HandleRotationMovement(); if ( _LockCursor && Input.GetMouseButtonUp(0)) { Cursor.lockState = m_LockCursor ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !m_LockCursor; } } private void OnDisable() { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } protected override void FollowTarget(float deltaTime) { if (m_Target == null) return; transform.position = Vector3.Lerp(transform.position, m_Target.position, deltaTime*m_MoveSpeed); } private void HandleRotationMovement() { if(Time.timeScale < float.Epsilon) return; var x = CrossPlatformInputManager.GetAxis("Mouse X"); var y = CrossPlatformInputManager.GetAxis("Mouse Y"); m_LookAngle += x*m_TurnSpeed; m_TransformTargetRot = Quaternion.Euler(0f, m_LookAngle, 0f); if (m_VerticalAutoReturn) { m_TiltAngle = y > 0 ? Mathf.Lerp(0, -m_TiltMin, y) : Mathf.Lerp(0, m_TiltMax, -y); } else { m_TiltAngle -= y*m_TurnSpeed; m_TiltAngle = Mathf.Clamp(m_TiltAngle, -m_TiltMin, m_TiltMax); } m_PivotTargetRot = Quaternion.Euler(m_TiltAngle, m_PivotEulers.y , m_PivotEulers.z); if (m_TurnSmoothing > 0) { m_Pivot.localRotation = Quaternion.Slerp(m_Pivot.localRotation, m_PivotTargetRot, m_TurnSmoothing * Time.deltaTime); transform.localRotation = Quaternion.Slerp(transform.localRotation, m_TransformTargetRot, m_TurnSmoothing * Time.deltaTime); } else { m_Pivot.localRotation = m_PivotTargetRot; transform.localRotation = m_TransformTargetRot; } } } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

勘で回答しますがUpdateメソッド内のHandleRotationMovement();の行を
if (Input.GetMouseButton(0)) HandleRotationMovement();にしてみてください。
(その次の行も直さないとロック周りがおかしくなるかも?)
Input-GetMouseButton - Unity スクリプトリファレンス

投稿2019/05/24 10:14

sakura_hana

総合スコア11425

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

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

Y0241-N

2019/05/27 00:41

if文の追加で入力時のみ視点回転にすることが出来ました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問