Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by UnityPlum · Apr 20, 2021 at 09:32 PM · buildunity 2dunity5unityeditorbullet

Bullet in editor does not behave the same in build.

Hello! This is going to be my first question so leave responses telling me how I can improve it.

I am trying to make a clone of the game RotMG (Realm of the Mad God) and I am starting to work on multiplayer. However, I encountered a weird bug that I came here to solve. I have implemented shooting and equipment, however, there seems to be a problem with either unity or my code. I have a basic projectile class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : MonoBehaviour
 {
     public bool ShotByPlayer;
     public int Damage;
     public float LifeSpan;
     public bool IsBase;
     public bool KilledItself = false;
 
     public int ShooterID;
     
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
     }
 
     public void SetSprite(Sprite newSprite) {
         gameObject.GetComponent<SpriteRenderer>().sprite = newSprite;
     }
 
     void OnTriggerEnter2D(Collider2D OtherCollider) {
         if(OtherCollider.gameObject.GetInstanceID() == ShooterID) {
             return;
         }
         GameObject EventManagerHost = GameObject.FindGameObjectsWithTag("EventManagerHost")[0];
         EventManagerHost.GetComponent<EventManager>().ProjectileHit(gameObject, OtherCollider.gameObject);
         if(IsBase == false)
             Destroy(gameObject);
     }
 }

And it seems to work. Everytime a player clicks the following method is called

 public void CreateBullet(Vector2 Position, Vector2 Velocity, int Damage, Sprite sprite, bool ShotByPlayer, Vector2 Size, float Range, int Id, Quaternion Rotation) {
         GameObject Bullet = Instantiate(GameObject.FindGameObjectsWithTag("BaseProjectile")[0]);
         Bullet.tag = "Projectile";
         Bullet.transform.position = Position;
         Bullet.GetComponent<Rigidbody2D>().velocity = Velocity;
         Bullet.GetComponent<Projectile>().ShotByPlayer = ShotByPlayer;
         Bullet.GetComponent<Projectile>().SetSprite(sprite);
         Bullet.GetComponent<SpriteRenderer>().size = Size;
         Bullet.GetComponent<Projectile>().Damage = Damage;
         Bullet.GetComponent<Projectile>().ShooterID = Id;
         Bullet.GetComponent<Projectile>().IsBase = false;
         Bullet.transform.rotation = Rotation;
         Bullet.transform.SetParent(GameObject.FindGameObjectsWithTag("ProjectileContainer")[0].transform); 
 
         Destroy(Bullet, Range / 23);
     }


So it sets the basic characteristics of a bullet: position, velocity, rotation, damage, sprite, size, and lifetime (Range)

And it seems to work fine in my editor, when I shoot it works fine and destroys itself after its lifetime is over. However, when I go to build my game and run it, it stops working (Kind of).

it still shoots and a bullet is created that looks fine and SOMETIMES destroys itself on time, however, oddly enough, only when I shoot forward, the bullet prematurely destroys itself and only when I shoot forward.

Can someone please explain why this is and how I can fix it?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityPlum · Apr 21, 2021 at 03:05 PM 0
Share

Worth noting that the bullet has a rigid body (kinematic) and box collider 2d.

avatar image UnityM0nk3y · Apr 22, 2021 at 02:35 PM 0
Share

I know you don't want to hear this, but consider ObjectPooling, rather than just "raw instantiating". And to simplify it, add a timer on the pooled projectile to switch itself off.

avatar image UnityPlum UnityM0nk3y · Apr 22, 2021 at 03:35 PM 0
Share

I am pretty sure that is what I am doing. I have a gameObject called "BaseProjectile" and then I just copy it using instantiate and set its fields. Am I wrong in doing this?

avatar image UnityPlum · Apr 22, 2021 at 03:35 PM 0
Share

I am pretty sure that is what I am doing. I have a gameObject called "BaseProjectile" and then I just copy it using instantiate and set its fields. Am I wrong in doing this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by UnityPlum · Apr 22, 2021 at 05:33 PM

So after A LOT of work, I managed to make a solution. To all of the game objects that I want bullets to interact with I added a BLANK script to it called "CanBeHit" with no code. Then, to my OnTriggerEnter2D method, I added this line of code.

 if(Mathf.Round(transform.rotation[3] * 10) / 10 == 0.7 && 
     Mathf.Round(transform.rotation[2] * 10) / 10 == 0.7 && 
     OtherCollider.gameObject.GetComponent<CanBeHit>() == null) {
             return;
 }

This basically checks if I am shooting up and if I am, and the object that we collided with cannot be hit, then just return, and keep ongoing. Apparently, the issue is that whenever I shot upwards, there was some rogue collider in its way that caused it to prematurely get destroyed.

So I checked if the object that we hit and if it was a rogue collider and we are aiming upwards, flat out ignore it.

Still not sure why the rogue collider only affects bullet in build and not in editor though.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Having trouble with Position X location on objects 0 Answers

Why is my admob banner ad not showing ?? 2 Answers

Lines drawn with LineRenderer is covered up by other game objects regardless of sorting order/positioning 1 Answer

Finding the Problematic dll in "Extracting Referenced dlls Failed" (Unity5) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges