MLAPI その10
なんとなくわかってきた。昨日のやつで配慮できてなかったところをやる。
全弾がアクティブだったときに、更にスペースキーを押すと null が返ってきてしまうので、if で囲う。
public class PlayerCubeController : MLAPI.NetworkedBehaviour { [MLAPI.Messaging.ServerRPC] private void Shoot() { GameObject bullet = spawnManager.GetFromPool(new Vector3(Random.Range(-9, 9), 1.0f, Random.Range(-9, 9)), Quaternion.Euler(0, 0, 90)); if (bullet != null) { bullet.GetComponent<MLAPI.NetworkedObject>().Spawn(); StartCoroutine(BackToPool(bullet, 2.0f)); } } }
Bullet の方で衝突時に自発的に消えてしまう部分をコメントアウト。
弾が衝突したときの影響がオブジェクトプールから取り出したあともまだ残っているので、ゼロにしたうえで戻す。
あと AddForce じゃなくて velocity に直接入れる形にしてみた。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { private float speed = 5; private Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void FixedUpdate() { rb.velocity = transform.up * speed; } void OnCollisionEnter(Collision other) { //Destroy(this.gameObject); } private void OnDisable() { if (rb) { rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; } } }
なんとなく形にはなったけど、とりあえず練習という感じで、もう少しやりたい方向に近づけるようにしたい。