OnTriggerEnter changing the way my projectiles instantiate?
Options
Hi! I'm working on a small multiplayer game and I want a player to be able to shoot a projectile.
It works well but I noticed that when I get rid of the code that handles the collision in the projectile script, the player shoots two projectiles in quick succession instead of one?
And I really can't understand why those would even be related.
Any help is appreciated! Thanks!
It works well but I noticed that when I get rid of the code that handles the collision in the projectile script, the player shoots two projectiles in quick succession instead of one?
And I really can't understand why those would even be related.
Any help is appreciated! Thanks!
public class Projectile : MonoBehaviour { public float speed = 10f; public float damage = 12f; private Rigidbody2D RB; private PhotonView view; [HideInInspector] public PhotonView shooterView; void Start() { view = GetComponent<PhotonView>(); RB = GetComponent<Rigidbody2D>(); } void Update() { Vector2 newPos = new Vector2(RB.position.x + 1 * speed * Time.deltaTime, RB.position.x); transform.Translate(Vector2.up * speed * Time.deltaTime); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { Debug.Log("Shot"); PhotonView playerView = collision.GetComponent<PhotonView>(); if (playerView != null) { if (playerView.IsMine != shooterView) { playerView.RPC("TakeDamage", RpcTarget.All, damage); PhotonNetwork.Destroy(gameObject); } } } } }
public class DefaultM1 : MonoBehaviour { [SerializeField] private float castTime = .1f; [SerializeField] private float coolDown = 1f; [SerializeField] private float damage = 12f; [SerializeField] private float speed = 12f; [SerializeField] private GameObject projectile = null; [SerializeField] private Transform shootPoint = null; private GameObject playerSprite; private PhotonView view; private float timeStamp; void Start() { view = GetComponent<PhotonView>(); playerSprite = GetComponent<PlayerShoot>().playerSprite; } void Update() { if (!view.IsMine) return; if (Input.GetMouseButton(0)) { if (timeStamp <= Time.time) { timeStamp = Time.time + coolDown; StartCoroutine(CastTime()); } } } private IEnumerator CastTime() { yield return new WaitForSeconds(castTime); view.RPC("Shoot", RpcTarget.All); StopCoroutine(CastTime()); } [PunRPC] private void Shoot() { GameObject obj = PhotonNetwork.Instantiate(projectile.name, shootPoint.position, playerSprite.transform.rotation); Projectile proj = obj.GetComponent<Projectile>(); proj.shooterView = view; proj.damage = damage; proj.speed = speed; } }
0