Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by drudiverse · Apr 19, 2014 at 03:36 PM · physicsrigidbodybulletefficiency

Bullet Management: Possible Without Rigidbody?

What are the correct methods of bullet management for 3d top scrolling shooter like "Raiden", "R-Type", "Gunbird" ? I want to make some lightweight code for bullets, without rigidbody, so the bullet just uses "bullet.transform.position = transform.forward*speed;" it moves forwards until it hits a collider, then the bullet can use "on collision enter" to play a sound. I dont think i need rigidbody, and those things? Is it wrong to make bullets without rigidbody?

I have a spacecraft flying in a straight tube very fast, and if i use physics, it isnt made for very fast bullets, it's more for bouncing cars around. The bullets would take up too much processor, would miss collisions, because they dont need drag, rotation etc?

What is the computationally simple way to detect collider intersections?

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 robertbu · Apr 19, 2014 at 03:41 PM 1
Share

Either your bullet or your target needs a Rigidbody to register a collision, and both must have a collider. $$anonymous$$oving objects by directly manipulating the transform teleports them from one position to the next, so there is a chance (which goes up with speed), that some collisions will not register. You can get around this kind of problem using Raycasting, or by directly detecting a collision with something like Physics.OverlapSphere(), but neither can be considered light weight.

avatar image grahnzz · Apr 19, 2014 at 03:51 PM 1
Share

One simple solution would be to check with Physics.Raycast or Physics.Spherecast if the bullet will hit the target. Either via a script on the camera that shoots the bullet or directly from the bullet in its forward direction. Be carefull with that seccond option though, make sure you Destroy() bullets that hit objects and bullets that just have been living for to long.

avatar image _dns_ · Apr 19, 2014 at 04:12 PM 1
Share

Hi, One of the 2 (potentially) colliding objects must have a rigidbody for collisions callbacks to be triggered. What I use in a similar game is a sphere collider for each bullet and some box/sphere/other colliders plus rigidbody for ships. The important thing is to have physics layers setup for enemy and player's bullets and enemy and player ship so the physics engine can optimize tests (= no need to test player's bullets against enemy bullets, unless you want this to happen in your game, use edit/project settings/physics). You can also freeze rotation and zero gravity if you don't use it.

To not miss collisions due to high speed: try reducing the fixed time interval to 60Hz. You can also use rigidbody.moveposition to ask the physics engine for more accuracy.

Unity's engine is already quite optimized and if a faster/simple way would exist, I guess they would use it. If you want to have your own "high speed proof" collisions detection, you have to test collisions for the object's "movement volume", not only the object's collider. That is, for a sphere, you have to test the capsule volume that represent the volume the sphere will cover when going from point A to B between 2 fixedupdates. I guess that's what rigidbody.moveposition does. This becomes more complicated for box colliders or other colliders. Physics.SphereCast may help to implement this for spheres, this should be enough for high speed bullets against ships.

I would try with unity's engine first before coding my own system. Their system is already in c++ and optimized with space partitioning algorithms like "sweep and prune" and layer masks that you would have to re-implement.

avatar image drudiverse · Apr 19, 2014 at 04:41 PM 0
Share

Wow thanks ALOT those are brilliant advice. i found complementary information, it said triggers can work without collision from eric5h5 and to use rigidbody except tick "is kinematic" so it switches off physics and only use transform controls.

found this page with advice about physics settings and timestep and about making collider same width as distance moved between frames: http://answers.unity3d.com/questions/52455/issues-with-continuous-dynamic-collision.html

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Khyrid · Jul 05, 2017 at 01:21 PM

Nobody answered you, but your question is a good one. Often using the rigidbodies in Unity takes precise control away from the developer. You may have already found your answer long ago, but for others like myself that wondered the same thing here is what works.

Set Projectile rigidbody to Is Kinematic in the inspector. This keeps it from being affected by physics but allows trigger collisions.

Set the colliders you intend for the projectile to hit to "Is Trigger"

For the code of shooting the bullet here is a sample of what I used (C#);

   public float projSpeed = 12.0f;
 
  

 void Fire ()
  {
        // Create the Bullet from the Bullet Prefab
         var bullet = (GameObject)Instantiate(
             bulletPrefab,
             bulletSpawn.position,
             bulletSpawn.rotation);
 
    //Ignore collision (Not sure if needed for kinematic)
     Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>());
 
    //Set speed of bullet
    bullet.GetComponent<Bullet>().bulletSpeed = projSpeed;
 
    // Destroy the bullet after 2 seconds
        Destroy(bullet, 2.0f); 
 }

For the Bullet itself;

 public float bulletSpeed = 0.0f;
 public Vector3 myDir;
 public float speed = 30.0f; //Probably don't need a slerp for this
 private Transform target;

 void Start()
 {
     //Find object I want bullets to move towards, in this case it is my 
     //crosshairs on the screen "CHWave"
     Transform nearestT; nearestT = GameObject.Find("CHWave").transform;
     target = nearestT;
     Vector3 vectorToTarget = target.position - transform.position;
     float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
     Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
     transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
 }

 void Update()
 {
     //Move Projectile
     transform.position += transform.right * bulletSpeed*Time.deltaTime;
 }

 void OnTriggerEnter(Collider other)//void OnCollisionEnter(Collision hit)
 {
     if (other.gameObject.tag == "PlayerTarget")// || hit.gameObject.tag == "Bullet")
     {
         // Physics.IgnoreCollision(other.GetComponent<Collider>(), GetComponent<Collider>());
         Physics.IgnoreCollision(other.GetComponent<Collider>(), GetComponent<Collider>());
         return;
     }

     var otherObj = other.gameObject;
     var health = otherObj.GetComponent<HealthRTS>();
     if (health  != null)
     {
         health.TakeDamage(10);
     }

     Destroy(gameObject);
 }






Comment
Add comment · Show 1 · 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
avatar image Khyrid · Jul 05, 2017 at 01:23 PM 0
Share

*Note I set $$anonymous$$e to move transform.right because of the way my top down game works. You will probably need transform.forward.

avatar image
0

Answer by Ambrose998800 · Jul 05, 2017 at 01:51 PM

I use particles ever since... You can control spread, simulate grain, speed, gravity, bouncing with speed loss, subemitters for splinters, sending message to collider for damage and decals... no spam of bullet-gameobjects while autofiring thousands of rounds. Now visualisation is easy, you can make tracers and much more.

Of course you can grab position and speed of every single particle... script-examples are in the web.

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
avatar image
0

Answer by Vollmondum · Jul 05, 2017 at 02:04 PM

Check if (bullet.position - gun.position) >= (target.position - gun.position). 2. Check if X/Z coordinate of the bullet is inside preset area (which is target.position +/- its radius)

On both true the will be a headshot. Destroy, decrease HP and voilah.

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

25 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

Related Questions

Best way to shoot physical bullets? 2 Answers

RigidBody.MovePosition seems jerky in movement 0 Answers

Any way to have a rigidbody not be affected by forces, but still collide with other objects? 2 Answers

Active rigidbodies and number of Contacts 1 Answer

Why is there no Rigidbody2D.SweepTest() ? 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