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

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

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

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

Q&A

解決済

1回答

1495閲覧

[Unity3D] cinemachine カメラが対象を追いかけてマウスを左右の動かすとカメラがプレイヤーを中心に回転する動作を実装したい。

退会済みユーザー

退会済みユーザー

総合スコア0

Unity3D

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

0グッド

1クリップ

投稿2022/03/19 07:53

編集2022/03/19 08:45

提示プロパティですが以下の動作を実現したいのですが色々パラメーターを触りましたが方法がわかりません。これはどうやって実装するのでしょうか?現状ではキャラクターが動くとカメラがキャラクターの背後に合わってしまいやりたい事とは違います。左右は実装出来たのですが上下が実装出来ません。提示画像の赤い円の上下版みたいな操作を実装したいです。

実現したい事

モンハン、マリオ64等であるプレイヤーを中心にカメラが回転移動して、カメラから見た方向にプレイヤーが移動するというカメラ処理です。

参考サイト:https://qiita.com/vodef6/items/e107a110d16f02d6530a
参考サイト:https://light11.hatenadiary.com/entry/2019/10/24/220542

イメージ説明
イメージ説明

Camera

cs

1using System.Collections; 2using System.Collections.Generic; 3using Cinemachine; 4using UnityEngine; 5 6public class CameraControl : MonoBehaviour 7{ 8 public GameObject player; 9 //public CinemachineVirtualCamera camera; 10 public float rotateSpeed = 5; //回転速度 11 12 private Vector3 offset; //プレイヤーとの距離 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 //camera = GetComponent<CinemachineVirtualCamera>(); 18 19 offset = player.transform.position; 20 } 21 22 // Update is called once per frame 23 void Update() 24 { 25 Rotate(); //カメラ回転 26 } 27 28 void LateUpdate() 29 { 30 Move(); //追尾 31 32 } 33 /*########################################## 追尾 ##########################################*/ 34 private void Move() 35 { 36 //transform.position += player.transform.position - offset; 37 offset = player.transform.position; 38 39 40 } 41 42 /*########################################## 回転 ##########################################*/ 43 private void Rotate() 44 { 45 float inputHorizontal = Input.GetAxis("Right_Horizontal"); 46 float inputVertical = Input.GetAxis("Right_Vertical"); 47 48 49 Vector3 angle = new Vector3(inputHorizontal, 0, inputVertical); 50 51 52 if(angle.magnitude > 0.4) 53 { 54 //GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineOrbitalTransposer>().m_Heading.m_Bias += inputHorizontal * rotateSpeed; //Biasを操作 55 GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineOrbitalTransposer>().m_FollowOffset.y -= inputVertical * rotateSpeed; //Biasを操作 56 //transform.RotateAround(player.transform.position, Vector3.up, inputHorizontal * rotateSpeed); 57 transform.RotateAround(player.transform.position, transform.right, inputVertical * rotateSpeed); 58 } 59 } 60} 61 62
Player

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerControl : MonoBehaviour 6{ 7 8 public GameObject camera; 9 [SerializeField] const float walkSpeed = 10; //移動速度 10 [SerializeField] const float fallSpeed = 15; //落下速度 11 12 private Vector3 moveSpeed; 13 private CharacterController controller; 14 private Animator animator; 15 16 17 private GameObject nowWeapon; 18 // Start is called before the first frame update 19 void Start() 20 { 21 controller = GetComponent<CharacterController>(); 22 animator = GetComponent<Animator>(); 23 24 nowWeapon = transform.Find("Weapon").GetComponent<WeaponManager>().Weapon_1(); 25 } 26 27 // Update is called once per frame 28 void Update() 29 { 30 Move(); 31 Fall(); 32 33 34 Vector3 move = Vector3.Scale(moveSpeed,new Vector3(1,0,1)); 35 36 animator.SetFloat("moveSpeed",move.magnitude); 37 animator.SetBool("isWeapon",false); 38 39 ChangeWeapon(); 40 controller.Move(moveSpeed * Time.deltaTime); //移動 41 } 42 43 44 45 /*########################################## アニメーション ##########################################*/ 46 private void Animation() 47 { 48 49 } 50 51 /*########################################## 武器 切り替え ##########################################*/ 52 private void ChangeWeapon() 53 { 54 55 if(Input.GetKeyDown(KeyCode.Alpha2) == true) 56 { 57 nowWeapon = transform.Find("Weapon").GetComponent<WeaponManager>().Weapon_2(); 58 transform.Find("Weapon").GetComponent<WeaponManager>().ChangeWeapon(); 59 } 60 } 61 62 /*########################################## 移動 ##########################################*/ 63 private void Move() 64 { 65 float inputHorizontal = Input.GetAxis("Horizontal"); 66 float inputVertical = Input.GetAxis("Vertical"); 67 68 var horizontalRotation = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y,Vector3.up); 69 var velocity = horizontalRotation * new Vector3(inputHorizontal, 0, inputVertical).normalized; 70 71 if (velocity.magnitude > 0.1f) 72 { 73 transform.rotation = Quaternion.LookRotation(velocity, transform.up); 74 moveSpeed = velocity * walkSpeed; 75 } 76 else 77 { 78 moveSpeed = new Vector3(0,0,0); 79 } 80 } 81 82 83 /*########################################## 落下 ##########################################*/ 84 private void Fall() 85 { 86 87 moveSpeed.y = -fallSpeed; 88 89 } 90} 91

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

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

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

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

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

guest

回答1

0

ベストアンサー

モンハン、マリオ64等であるプレイヤーを中心にカメラが回転移動して、カメラから見た方向にプレイヤーが移動するというカメラ処理です。

マリオ64は自分でカメラを操作することはないと思いますし、モンハンでも「カメラから見た方向にプレイヤーが移動する」わけではなくて「カメラとの相対軸(を修正した)方向に移動する」のですが、マウスもしくは右スティックでカメラを動かす機能は Cinemachine にあります。Freelook Camera というテンプレートを使って設定すればよいです。

投稿2022/03/19 08:50

編集2022/03/19 09:09
bboydaisuke

総合スコア5275

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問