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

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

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

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

Unity3D

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

Unity

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

Q&A

解決済

1回答

1464閲覧

unityでカメラの向いている方向にキャラを動かしたいです。

isshiisshi

総合スコア1

C#

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

Unity3D

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

Unity

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

0グッド

0クリップ

投稿2022/11/22 13:30

前提

Unity初心者です。
現在Unityでアクションゲームを作ろうとしています。

実現したいこと

カメラにが向いている方向(写っている画面の方向)を進行方向(z方向)としてキャラクターを動かしたいのですが、やり方がわかりません。
(ワールドの絶対的な前後左右にむかって移動するのではなく、カメラの向きや位置によってその時の前後左右が変わるようにしたいです。3Dマリオのようなイメージです。)

また、プレイヤーはキャラクターコントローラーを使って移動させたいと考えています。

現状カメラはプレイヤーを中心に回転し、追従するようにしています。
ソースコードを貼っておきます。

カメラの制御スクリプト自体はおそらく間違っていないと思うのですが、プレイヤーの移動のスクリプトをどう組めば、カメラに写っている方向を進行方向として移動できるのかがわかりません。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Camerafollow : MonoBehaviour 6{ 7 public GameObject target; 8 Vector3 targetPosi; 9 10 11 float angle; 12 public float RotateSpeed; 13 14 15 16 // Start is called before the first frame update 17 void Start() 18 { 19 targetPosi=target.transform.position; 20 } 21 22 private void Update() 23 { 24   //追従処理 25 transform.position += target.transform.position-targetPosi; 26 targetPosi =target.transform.position; 27 28  //回転処理 29 angle = Input.GetAxis("Horizontal2") * RotateSpeed; 30//Horizontal2はPS4コントローラーの右スティックの入力に対応させています。 31 32 transform.RotateAround(targetPosi, Vector3.up, angle); 33 }

試したこと

以下の移動スクリプトを書いたのですがうまく動かないという状況です。
おそらくこの移動に関して間違いがあると思うのですが、どこがどう間違えているかわからないという状況です。

C#

1public class PlayerMove : MonoBehaviour 2{ 3 4 [SerializeField] private float moveSpeed = 3; 5 6 private CharacterController _characterController; 7 8 public Vector3 _moveVelocity; 9 10 [SerializeField] private GameObject Camera; 11 12 float inputHorizontal; 13 float inputVertical; 14 15 16 void Start() 17 { 18 _characterController = GetComponent<CharacterController>(); 19 20 } 21 22 void Update() 23 { 24 if (_characterController.isGrounded) 25 { 26 27   //入力された方向にスピードをかける 28 inputHorizontal = Input.GetAxis("Horizontal") * moveSpeed; 29 inputVertical = Input.GetAxis("Vertical") * moveSpeed; 30 31 32   //カメラの向きをもったベクトルに上で算出されたものをかける 33 _moveVelocity = Camera.transform.forward * inputVertical + Camera.transform.right * inputHorizontal; 34 35 36 37 //入力された方向に向く 38 _transform.LookAt(_transform.position + new Vector3(_moveVelocity.x, 0, _moveVelocity.z)); 39 40 41 42 43 _characterController.Move(_moveVelocity * Time.deltaTime); 44 45 } 46}

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

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

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

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

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

Sobasenbei

2022/11/22 16:27

キャラクターとカメラは親子関係にありますか
isshiisshi

2022/11/23 02:49

いえ、ありません!
isshiisshi

2022/11/23 02:54

親子関係ではないですが、プレイヤー側のインスペクター上で参照しています。
isshiisshi

2022/11/23 03:06

移動のスクリプトですが、正確にはこう書いていました。 public class PlayerMove : MonoBehaviour { if (_characterController.isGrounded) { [SerializeField] private float moveSpeed = 3; private CharacterController _characterController; public Vector3 _moveVelocity; [SerializeField] private GameObject Camera; float inputHorizontal; float inputVertical; void Start() { _characterController = GetComponent<CharacterController>(); } void Update() { if (_characterController.isGrounded) {    //入力された方向にスピードをかける inputHorizontal = Input.GetAxis("Horizontal") * moveSpeed; inputVertical = Input.GetAxis("Vertical") * moveSpeed;    //カメラの向きをもったベクトルに上で算出されたものをかける _moveVelocity = Camera.transform.forward * inputVertical + Camera.transform.right * inputHorizontal; //入力された方向に向く _transform.LookAt(_transform.position + new Vector3(_moveVelocity.x, 0, _moveVelocity.z)); } else { _moveVelocity.x = 0; _moveVelocity.z = 0; _moveVelocity.y += Physics.gravity.y * Time.deltaTime; } _characterController.Move(_moveVelocity * Time.deltaTime); } }
isshiisshi

2022/11/23 03:08

if (_characterController.isGrounded) { の部分と else { _moveVelocity.x = 0; _moveVelocity.z = 0; _moveVelocity.y += Physics.gravity.y * Time.deltaTime; } が本当は書いていたのですが、質問の時に省いた部分です。
Sobasenbei

2022/11/23 03:49

質問文の方を変更してもらえると助かります Camera.transform.forward などの値はちゃんと取れていますか? 関係ないかもしれませんがLookAtは使い方が間違っているように思えます
isshiisshi

2022/11/24 04:42

すいません、いろいろ自分でコードをいじっている間に、解決できてしまいました。 else内のコードが不要であったり、ベクトルをうまく取得できていなかったようです。 Sobasenbeiさん、ご返信ありがとうございました!
guest

回答1

0

自己解決

以下のコードで解決いたしました。

public class PlayerMove : MonoBehaviour { [SerializeField] private float jumpPower = 3; [SerializeField] private float moveSpeed = 3; private CharacterController _characterController; private Transform _transform; public Vector3 _moveVelocity; Vector3 cameraForward; [SerializeField] private GameObject Camera; float inputHorizontal; float inputVertical; float groundtime; bool isgrounded; // Start is called before the first frame update void Start() { _characterController = GetComponent<CharacterController>(); _transform = GetComponent<Transform>(); } // Update is called once per frame void Update() { if (_characterController.isGrounded) { //_characterController.isGroundedの精度が悪いため(フレーム毎に接地判定されたりされなかったりする。) //ので時間によって接地判定。0.1秒以上接地がなかったとすると空中判定となる groundtime = 0.0f; isgrounded = true; //方向入力 inputHorizontal = Input.GetAxis("Horizontal") ; inputVertical = Input.GetAxis("Vertical") ; //カメラ前方のベクトルのxz成分を取得して、単位ベクトル化 cameraForward =Camera.transform.forward; cameraForward.y=0; cameraForward = cameraForward.normalized; //方向の入力に応じて動く方向を決める _moveVelocity = cameraForward*inputVertical+Camera.transform.right*inputHorizontal; //動く方向を向く _transform.LookAt(_transform.position + new Vector3(_moveVelocity.x, 0, _moveVelocity.z)); } else { groundtime += Time.deltaTime; if (groundtime >= 0.1f) { isgrounded = false; } /* _moveVelocity.x = 0; _moveVelocity.z = 0;*/ _moveVelocity.y += Physics.gravity.y * Time.deltaTime; } if (Input.GetButtonDown("Jump") && isgrounded == true) { _moveVelocity.y = jumpPower; } _characterController.Move(_moveVelocity * moveSpeed * Time.deltaTime); } }

投稿2022/11/24 04:45

isshiisshi

総合スコア1

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問