前提・実現したいこと
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); } }