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

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

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

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

Unity

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

Q&A

1回答

1650閲覧

3Dシューティングゲームの弾丸の制御について

RO_456

総合スコア2

C#

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

Unity

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

0グッド

0クリップ

投稿2020/04/22 12:01

編集2020/04/22 13:16

前提・実現したいこと

3Dシューティングゲームを作っているのですが、弾丸を発射した後、下記の動画のように自機を移動させると、弾も左右に移動してしまいます。
真っ直ぐ飛ぶようにしたいのですが、ソースコードのb.transform.parent= bulletParent;を外すとPlayerの動きの都合上、大きくマズルがずれてしまいます。
代替策か、左右への移動を無効にする方法を知りたいです。

発生している問題・エラーメッセージ

動画のように、発射した後に左右に自機を動かすと弾も動いてしまう。
b.transform.parent= bulletParent;を外した場合

該当のソースコード

using System.Collections; using System.Collections.Generic; using UnityEngine; public class muzzle : MonoBehaviour { private Transform bulletParent; public GameObject bullet; private GameObject b; // Start is called before the first frame update public float speed = 500; void Start() { bulletParent = GameObject.FindGameObjectWithTag("bulletParent").transform; } // Update is called once per frame private void Update() { if (Input.GetButtonDown("Fire1")) { b = (GameObject)Instantiate(bullet, transform.position, transform.rotation); b.transform.parent = bulletParent; // b.transform.Translate(Vector3.forward * speed); b.GetComponent<Rigidbody>().AddForce(transform.forward * 500, ForceMode.Impulse); //speed } } }

追記

質問への追記・修正の依頼ありがとうございます。
このプロジェクトはStarFox-RailMovementというツールを使用したものとなっています。
恐らくはmuzzleもPlayerの子にあるため、つまりはPlayerとしての挙動を得てしまっているため、このようなバラバラの挙動になるものと思われます。

イメージ説明
親の層にある「Player」が、Playerを動かすPlayerMove.csがアタッチされています。
以下、PlayerMove.csの内容です。

PlayerMove.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using Cinemachine; //using UnityEngine.Rendering.PostProcessing; public class PlayerMovement : MonoBehaviour { private Transform playerModel; [Header("Settings")] public bool joystick = true; [Space] [Header("Parameters")] public float xySpeed = 18; public float lookSpeed = 340; public float forwardSpeed = 6; [Space] [Header("Public References")] public Transform aimTarget; public CinemachineDollyCart dolly; public Transform cameraParent; [Space] [Header("Particles")] public ParticleSystem trail; public ParticleSystem circle; public ParticleSystem barrel; public ParticleSystem stars; void Start() { playerModel = transform.GetChild(0); SetSpeed(forwardSpeed); } void Update() { float h = joystick ? Input.GetAxis("Horizontal") : Input.GetAxis("Mouse X"); float v = joystick ? Input.GetAxis("Vertical") : Input.GetAxis("Mouse Y"); LocalMove(h, v, xySpeed); RotationLook(h,v, lookSpeed); HorizontalLean(playerModel, h, 80, .1f); if (Input.GetButtonDown("Action")) Boost(true); if (Input.GetButtonUp("Action")) Boost(false); if (Input.GetButtonDown("Fire3")) Break(true); if (Input.GetButtonUp("Fire3")) Break(false); if (Input.GetButtonDown("TriggerL") || Input.GetButtonDown("TriggerR")) { int dir = Input.GetButtonDown("TriggerL") ? -1 : 1; QuickSpin(dir); } } void LocalMove(float x, float y, float speed) { transform.localPosition += new Vector3(x, y, 0) * speed * Time.deltaTime; ClampPosition(); } void ClampPosition() { Vector3 pos = Camera.main.WorldToViewportPoint(transform.position); pos.x = Mathf.Clamp01(pos.x); pos.y = Mathf.Clamp01(pos.y); transform.position = Camera.main.ViewportToWorldPoint(pos); } void RotationLook(float h, float v, float speed) { aimTarget.parent.position = Vector3.zero; aimTarget.localPosition = new Vector3(h, v, 1); transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(aimTarget.position), Mathf.Deg2Rad * speed * Time.deltaTime); } void HorizontalLean(Transform target, float axis, float leanLimit, float lerpTime) { Vector3 targetEulerAngels = target.localEulerAngles; target.localEulerAngles = new Vector3(targetEulerAngels.x, targetEulerAngels.y, Mathf.LerpAngle(targetEulerAngels.z, -axis * leanLimit, lerpTime)); } private void OnDrawGizmos() { Gizmos.color = Color.blue; Gizmos.DrawWireSphere(aimTarget.position, .5f); Gizmos.DrawSphere(aimTarget.position, .15f); } public void QuickSpin(int dir) { if (!DOTween.IsTweening(playerModel)) { playerModel.DOLocalRotate(new Vector3(playerModel.localEulerAngles.x, playerModel.localEulerAngles.y, 360 * -dir), .4f, RotateMode.LocalAxisAdd).SetEase(Ease.OutSine); barrel.Play(); } } void SetSpeed(float x) { dolly.m_Speed = x; } void SetCameraZoom(float zoom, float duration) { cameraParent.DOLocalMove(new Vector3(0, 0, zoom), duration); } void DistortionAmount(float x) { // Camera.main.GetComponent<PostProcessVolume>().profile.GetSetting<LensDistortion>().intensity.value = x; } void FieldOfView(float fov) { cameraParent.GetComponentInChildren<CinemachineVirtualCamera>().m_Lens.FieldOfView = fov; } void Chromatic(float x) { // Camera.main.GetComponent<PostProcessVolume>().profile.GetSetting<ChromaticAberration>().intensity.value = x; } void Boost(bool state) { if (state) { cameraParent.GetComponentInChildren<CinemachineImpulseSource>().GenerateImpulse(); trail.Play(); circle.Play(); } else { trail.Stop(); circle.Stop(); } trail.GetComponent<TrailRenderer>().emitting = state; float origFov = state ? 40 : 55; float endFov = state ? 55 : 40; float origChrom = state ? 0 : 1; float endChrom = state ? 1 : 0; float origDistortion = state ? 0 : -30; float endDistorton = state ? -30 : 0; float starsVel = state ? -20 : -1; float speed = state ? forwardSpeed * 2 : forwardSpeed; float zoom = state ? -7 : 0; DOVirtual.Float(origChrom, endChrom, .5f, Chromatic); DOVirtual.Float(origFov, endFov, .5f, FieldOfView); DOVirtual.Float(origDistortion, endDistorton, .5f, DistortionAmount); var pvel = stars.velocityOverLifetime; pvel.z = starsVel; DOVirtual.Float(dolly.m_Speed, speed, .15f, SetSpeed); SetCameraZoom(zoom, .4f); } void Break(bool state) { float speed = state ? forwardSpeed / 3 : forwardSpeed; float zoom = state ? 3 : 0; DOVirtual.Float(dolly.m_Speed, speed, .15f, SetSpeed); SetCameraZoom(zoom, .4f); } }

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

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

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

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

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

fiveHundred

2020/04/22 12:53

親を動かすと子も動くようになっているので「b.transform.parent= bulletParent;」とすると、そのような挙動になります。 この行は削除するとして、マズルがずれてしまうのを直すことになると思いますが、そのずれてしまう原因は何かわかりますか? また、オブジェクトの親子関係と(あるのであれば)マズルを移動させているコードも記載すると、直す方法が分かるかもしれません。 あと、コードの表示が上手くいっていないので、その修正もお願いします。 (「```」の中に記載してください)
guest

回答1

0

b.transform.parent= bulletParent;を外すのは正しいです

ただ、後ろに行っているように見えるのは、弾の速度が0だからだと思います
自機から発射された弾は最初自機の速度を持った状態で発生するはずです
なので、最初に自機の速度を弾に与えてやればいいと思います

投稿2020/04/23 20:33

izmktr

総合スコア2856

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問