Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unity_9447unni9447 · May 20, 2020 at 04:23 PM · instantiateprefabshootingthird-person

take collision info from an instantiated bullet

Hello and sorry for my bad English. For almost a week I am my friend were developing a third person shooter game and we just encountered a problem with the shooting system. We want the bullet which is been spawned to collide and then check if the collided object has the enemy script in it or not within the gun script itself. and this is the Gun script. Also thanks in advance.

public class GunScript : MonoBehaviour { public float Damage; public float Range; public float FireRate; private float NextTimeToFire = 0; public ParticleSystem MuzzleFlash; public Camera TpsCam; public Animator PlayerAnimator;

 public Transform BulletSpawner;
 public Transform CrosshairLoc;
 public Rigidbody Projectile;
 void Start()
 {
    
 }

 void Update()
 {
     if(Input.GetMouseButtonDown(1)) PlayerAnimator.SetBool("Aim", true);
     if (Input.GetMouseButtonUp(1)) PlayerAnimator.SetBool("Aim", false);
     if (Input.GetButton("Fire1") && Time.time >= NextTimeToFire) Shoot();
 }

 public void Shoot()
 {
     NextTimeToFire = Time.time + 1 / FireRate;
     MuzzleFlash.Play();
     Ray ray = TpsCam.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
     RaycastHit hit;
     // Check whether your are pointing to something so as to adjust the direction
     Vector3 targetPoint;
     if (Physics.Raycast(ray, out hit))
         targetPoint = hit.point;
     else
         targetPoint = ray.GetPoint(1000); // You may need to change this value according to your needs
                                           // Create the bullet and give it a velocity according to the target point computed before
     var bullet = Instantiate(Projectile, BulletSpawner.transform.position, BulletSpawner.transform.rotation);
     bullet.GetComponent<Rigidbody>().velocity = (targetPoint - BulletSpawner.transform.position).normalized * 100;
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ADiSiN · May 20, 2020 at 11:00 PM

Hi!

I would suggest you for that to create script and attach it to your bullet prefab which you are instantiating. The script itself will simply check for collision and if detects any collision then it will check for additional information - in your specific case you want to check for enemy script, so it can look like this:

 void OnCollisionEnter(Collision collision)
 {
     EnemyScript eScript = collision.collider.GetComponent<EnemyScript>();
 
     if(eScript != null)
     {
         // Do something
     }
 }

However, you can create specific tag for your enemy and check for the tag:

 void OnCollisionEnter(Collision collision)
 {
     if (collision.collider.CompareTag("EnemyTag"))
     {
         // Do something
     }
 }

The decision is up to you.

After enemy detected you can call specific functions, instantiate effects, destroy bullet and other stuff.


I am not sure for what reason you would like it to be checked inside your Gun script itself since it will create kinda mess and you also would need to store all bullets in List to access all of them for checking of collisions which will still be essentially kinda the same thing, but overcomplicated.


Hope it helps.

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

185 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 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

Destorying prefab and instanstiating again? 0 Answers

Prefab shooting damage/health? 1 Answer

AddForce not working on instantiated bullets 1 Answer

How do I re-instantiate original prefab after a game object inside has been destroyed? 2 Answers

[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 Answers


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