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

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

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

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

Unity3D

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

オブジェクト

オブジェクト指向において、データとメソッドの集合をオブジェクト(Object)と呼びます。

Unity

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

Q&A

解決済

1回答

4009閲覧

Unityで敵キャラをロックオンしたまま動かしたいです

Um_kok

総合スコア39

C#

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

Unity3D

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

オブジェクト

オブジェクト指向において、データとメソッドの集合をオブジェクト(Object)と呼びます。

Unity

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

0グッド

0クリップ

投稿2020/08/24 07:35

UnityでプレイヤーがLボタンを押すと敵の方向を向くようにしているのですが、移動すると敵を向かなくなり、移動し終わると敵を向いてしまいます。
やりたいことは敵の方向を向いたまま移動したいです。最終的には移動しながら敵の方向に弾を撃てるようにするためです
二つスクリプトをアタッチしています
⇓が移動用のスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerMove : MonoBehaviour 6{ 7 private CharacterController controller; 8 private Vector3 playerVelocity; 9 private bool groundedPlayer; 10 private float playerSpeed = 2.0f; 11 private float jumpHeight = 1.0f; 12 private float gravityValue = -9.81f; 13 14 private void Start() 15 { 16 controller = gameObject.GetComponent<CharacterController>(); 17 } 18 19 void Update() 20 { 21 groundedPlayer = controller.isGrounded; 22 if (groundedPlayer & playerVelocity.y < 0) 23 { 24 playerVelocity.y = 0f; 25 } 26 27 Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 28 controller.Move(move * Time.deltaTime * playerSpeed); 29 30 if (move != Vector3.zero) 31 { 32 gameObject.transform.forward = move; 33 } 34 35 // Changes the height position of the player.. 36 if (Input.GetButtonDown("Jump") & groundedPlayer) 37 { 38 playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue); 39 } 40 41 playerVelocity.y += gravityValue * Time.deltaTime; 42 controller.Move(playerVelocity * Time.deltaTime); 43 } 44}

⇓がLキーを押されたとき敵の方向を向くスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class LockOn : MonoBehaviour 6{ 7 8 GameObject target; 9 10 // Start is called before the first frame update 11 void Start() 12 { 13 14 } 15 16 // Update is called once per frame 17 void Update() 18 { 19 if (Input.GetButtonDown("Lock")) 20 { 21 if (target == null) 22 { 23 target = GameObject.Find("Enemy"); 24 } 25 else 26 { 27 target = null; 28 } 29 } 30 if (target != null) 31 { 32 transform.LookAt(target.transform); 33 } 34 } 35} 36 37```** 38**回答お願いします(´;ω;)**

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

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

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

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

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

guest

回答1

0

ベストアンサー

  1. ロックオンしている状態
  2. CharacterControllerの移動

スクリプトを分けているので1のロックオン状態が2のCharacterControllerでの移動中に上書きされているんだと思います。スクリプトをまとめると機能すると思います。

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerMove : MonoBehaviour { 6 7 public GameObject target; 8 9 private CharacterController controller; 10 private Vector3 playerVelocity; 11 private bool groundedPlayer; 12 private float playerSpeed = 2.0f; 13 private float jumpHeight = 1.0f; 14 private float gravityValue = -9.81f; 15 16 private void Start() { 17 controller = gameObject.GetComponent<CharacterController>(); 18 } 19 20 void Update() { 21 groundedPlayer = controller.isGrounded; 22 if (groundedPlayer & playerVelocity.y < 0) { 23 playerVelocity.y = 0f; 24 } 25 26 Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 27 controller.Move(move * Time.deltaTime * playerSpeed); 28 29 if (move != Vector3.zero) { 30 gameObject.transform.forward = move; 31 } 32 33 // Changes the height position of the player.. 34 if (Input.GetButtonDown("Jump") & groundedPlayer) { 35 playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue); 36 } 37 38 if (Input.GetButtonDown("Lock")) { 39 if (target == null) { 40 target = GameObject.Find("Enemy"); 41 } else { 42 target = null; 43 } 44 } 45 46 if (target != null) { 47 transform.LookAt(target.transform); 48 } 49 50 playerVelocity.y += gravityValue * Time.deltaTime; 51 controller.Move(playerVelocity * Time.deltaTime); 52 53 } 54}

投稿2020/08/24 08:38

編集2020/08/24 08:40
hogefugapiyo

総合スコア3302

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

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

Um_kok

2020/08/24 08:55

なるほどです!一緒に処理したいスクリプトは一つにまとめるのですね。勉強になりました。こちらの書き間違えかもしれないのですが、なぜか最初から敵の方向を向いています。なぜでしょうか?
Um_kok

2020/08/24 09:02

すみません。解決しました!スクリプトでpublic GameObject targetと書いてあったため シリアライズ化されInspecterで設定できるようになっていたため、私がEnemyを入れてしまっていました。 貴重な回答ありがとうございました!これからも頑張ります!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問