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 Deathslice · Aug 02, 2017 at 05:25 PM · physicsrigidbodyraycastvector3

How do I make the bullet Tracer move towards the location of where the bullet hole prefab gets instantiated?

So I have this IEnumertator function called checkIfHit() that spawns two raycasts(one is exploratory and the other one instantiates a bullet hole if the raycast hits an enemy.) and a bullet tracer whenever the player presses the fire button. Here is the code for that function.

     private IEnumerator checkIfHit()
     {
         Vector3 randomDirection = getRandomBulletDirection();
         RaycastHit hit;
 
         // Cast an exploratory raycast first from a randomly generated direction.
         if(Physics.Raycast(bulletSpawnPoint.position, randomDirection, out hit, bulletRange))
         {
             // calculate how long it would take the bullet to get to its target.
             float delay = hit.distance / bulletSpeed;
             // calculate the bullet drop at the target.
             Vector3 hitPoint = hit.point;
             hitPoint.y -= delay * 9.8f;
             // calculate the new vector's direction
             Vector3 dir = hitPoint - bulletSpawnPoint.position;
             yield return new WaitForSeconds(delay);
 
             // spawn a bullet tracer from the direction(normalized) that was lowered to simulate a bullet drop.
             StartCoroutine(spawnBulletTracer(dir.normalized));
 
             // Now perform the actual shooting.
             if (Physics.Raycast(bulletSpawnPoint.position, dir, out hit, bulletRange))
             {
                 GameObject bulletHole = SpawningPool.CreateFromCache(bulletImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
                 yield return new WaitForSeconds(3.0f);
                 SpawningPool.ReturnToCache(bulletHole);
             }
         }
     }

To simulate gun inaccuracy, I first generate a random direction vector using this function.

     private Vector3 getRandomBulletDirection()
     {
         // Generate a random XY point inside a circle.
         Vector3 randomDirection = Random.insideUnitCircle * scaleLimit;
         randomDirection.z = spreadZDirection;
         randomDirection = transform.TransformDirection(randomDirection.normalized);
         return randomDirection;
     }

After the exploratory raycast hits something and I've calculated the direction for the second raycast, I immediately call the function spawnBulletTracer to instantiate it and make it move in the direction that the second raycast is going to. Here is the code for that function.

     private IEnumerator spawnBulletTracer(Vector3 direction)
     {
         // Retrieve from the spawn pool a cached bullet tracer object.
         GameObject bulletTracer = SpawningPool.CreateFromCache(tracer, bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.rotation);
 
         Rigidbody rigidbodyTracer = bulletTracer.GetComponent<Rigidbody>();
 
         rigidbodyTracer.velocity = transform.TransformDirection(direction * tracerSpeed);
 
         yield return new WaitForSeconds(1.5f);
         SpawningPool.ReturnToCache(bulletTracer, "Tracer");
         // Send it back to the cache so that it may be used later.
     }

If it helps, here is how my bulletTracer prefab looks like in the inspector.

Bullet Tracer Prefab settings

To me, each step that I've done in each of those function made sense to me so maybe there is a miscalculation somewhere(more than likely it's the vector that I passing to spawnBulletTracer that is not correct.). At the very least each bullet hole is being instantiated in different locations which is what I want but the tracer is not moving in the direction that you would expect. Help would be appreciated.

webpnet-compress-image.jpg (501.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bieere · Aug 02, 2017 at 06:16 PM

What is the current behaviour, as it will help determine what the problem might be.

Does it not spawn at all, does it stop short, or move in an incorrect direction?

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 Deathslice · Aug 02, 2017 at 06:42 PM 0
Share

The behavior that I want is not only for the bullethole to appear in random locations(to simulate bullets firing in different directions) but for the bullet tracer to also going in the correct direction. What I mean by correct direction is for example, if I shoot at the center of the screen and let us also say that I get generate a random vector that points to the center of the screen, then the bullethole prefab should be instantiated in the center screen and the tracer should move to the center of the screen as well.

The current behavior is the bullethole instantiates in a random location but the tracer is not moving in the correct direction(If the bullethole instantiates a little bit to the left, the bullet tracer might move to the right).

avatar image
0

Answer by Deathslice · Aug 02, 2017 at 08:43 PM

I solved my problem. Instead of doing

          rigidbodyTracer.velocity = transform.TransformDirection(direction * tracerSpeed);

I changed it to

          rigidbodyTracer.velocity = direction * tracerSpeed;

I not sure why it's working now. Maybe somebody here can explain why.

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

149 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

Related Questions

Raycast stops working after ~40 seconds when target has Rigidbody 2 Answers

Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer

instantiate an object on the intersection point between two objects on the x and y axis 1 Answer

Shoot Raycast to a specified Angle 0 Answers

Apply rigid body force in opposite direction of collider 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