提示コードですが以下のように弾をプレイヤーの方向に向けているにも関わらず弾が横を向いてしまう原因が知りたいです。弾がプレイヤーの方向を向く
にはどうすればいいのでしょうか?
cs
1now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position);
の一行では向いてくれません。
LookRotation: https://nekojara.city/unity-look-at#%E3%82%BF%E3%83%BC%E3%82%B2%E3%83%83%E3%83%88%E3%81%B8%E3%81%AE%E5%90%91%E3%81%8D%E3%81%AE%E8%A8%88%E7%AE%97
Bullet
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Enemy_Weapon : MonoBehaviour 6{ 7 [SerializeField] float speed; 8 9 private bool isShot = false; 10 private Vector3 vector; 11 12 13 public void Shot(Vector3 vec,Vector3 pos) 14 { 15 isShot = true; 16 vector = vec; 17 18 transform.position = pos; 19 } 20 21 22 23 24 25 // Start is called before the first frame update 26 void Start() 27 { 28 29 } 30 31 // Update is called once per frame 32 void Update() 33 { 34 if (isShot == true) 35 { 36 transform.Translate(vector * speed * Time.deltaTime); 37 } 38 39 } 40 41 public void OnTriggerEnter(Collider other) 42 { 43 if(other.gameObject.name == "Player") 44 { 45 transform.Find("Damage").GetComponent<ParticleSystem>().Play(); 46 //transform.Find("Object").gameObject.SetActive(false); 47 48 49 } 50 } 51 52 53} 54
Enemy
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Enemy : MonoBehaviour 6{ 7 [SerializeField] GameObject weapon; 8 9 10 private GameObject hitEffect; //ヒットエフェクト 11 private GameObject viewArea; //視界 12 13 private float time = 0; 14 // Start is called before the first frame update 15 void Start() 16 { 17 hitEffect = transform.Find("Hit").gameObject; 18 hitEffect.GetComponent<ParticleSystem>().Stop(); 19 20 viewArea = transform.Find("ViewArea").gameObject; 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 if(viewArea.GetComponent<ViewArea>().getIsInPlayer() == true) 27 { 28 transform.rotation = Quaternion.LookRotation(Vector3.Normalize(viewArea.GetComponent<ViewArea>().getPlayerVector()),Vector3.up); 29 30 time += Time.deltaTime; 31 if(time > 1) 32 { 33 Transform now = weapon.GetComponent<WeaponPool>().GetInstance(); 34 35 now.GetComponent<Enemy_Weapon>().Shot(viewArea.GetComponent<ViewArea>().getPlayerVector(), transform.position); 36///////////////////////////////////////////////////////////////// 37 now.transform.rotation = Quaternion.LookRotation(viewArea.GetComponent<ViewArea>().getPlayerPosition() - now.transform.position); 38///////////////////////////////////////////////////////////////// 39 40 time = 0; 41 } 42 } 43 } 44 45 46 47 public void OnCollisionEnter(Collision collision) 48 { 49 if(collision.gameObject.tag == "PlayerBullet") 50 { 51 //hitEffect.transform.position = collision.gameObject.transform.position; 52 hitEffect.GetComponent<ParticleSystem>().Play(); 53 } 54 } 55} 56
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/03/24 05:38 編集
2022/03/24 08:07
退会済みユーザー
2022/03/25 00:11