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 JaminEatWorld · Nov 05, 2015 at 01:05 PM · collisionmovementraycastvector3bounce

Issues with Bullet Ricochet

Hi there!

I'm having issues getting my bullet prefab to ricochet off a wall. I've already got a successful bouncing enemy that uses similar code, but because these enemies aren't controlled by user interaction - and head off in a set direction - they work (bounce) perfectly. It's another issue entirely for bullets fired by the player, however.

The issue - based on the code below - results in my bullets either firing correctly in correct direction but not bouncing upon hitting wall, OR bouncing correctly, but with the bullet ONLY firing forwards. Aiming elsewhere does nothing.

Here is the code that's on my bullet prefab:

    transform.position += transform.forward * speed * Time.deltaTime;
     
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit; 
         
         if (Physics.Raycast(ray, out hit, Time.deltaTime * speed + .1f,collisionMask))
         {
             Vector3 reflectDir = Vector3.Reflect(ray.direction, hit.normal); 
             float rot = 90 - Mathf.Atan2(reflectDir.z, reflectDir.x) * Mathf.Rad2Deg;
             transform.eulerAngles = new Vector3(0,rot,0);
         }

And here is the relevant code from my PlayerShooting script:

 if(Time.time >= coolDown && GameObject.Find ("Obelisk_Base_SE").GetComponent<ObeliskCommandSE> ().isActivated == false)
         {
             if(Input.GetMouseButton(0))
             {
                 NoShootAnkhAward = 20f;
                 FireBounce();
                 GetComponent<AudioSource> ().Play ();
                 numberOfBullets ++; 
             }
             
         }
 
     void FireBounce()
     {
         Rigidbody bPrefab = Instantiate(bulletPrefab,transform.position, Quaternion.identity) as Rigidbody;
         
         bPrefab.GetComponent<Rigidbody>().AddForce(transform.up * bulletSpeed);
         
         coolDown = Time.time + attackSpeed;
         
     }

From what I can tell, the issue seems to be a conflict between bPrefab.GetComponent().AddForce(transform.up bulletSpeed); from my PlayerShooting script, and transform.position += transform.forward speed Time.deltaTime;* from my ricochet code.

Any help would be super appreciated - I'm pretty new to Unity/C# and have got myself pretty confused over this one.

Many thanks!

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 brazmogu · Nov 06, 2015 at 12:51 PM

I'm not sure I understand completely what's going on, but assuming the AddForce is working as intended and shooting your bullet in the right direction, I think your problem is the direction of your Raycast.

The ray you're using for the cast is

 Ray ray = new Ray(transform.position, transform.forward);

That's a ray coming from the bullet's position and pointing to the front of the bullet's point of view. Problem is, I'm not seeing the bullet being rotated to match the player's direction anywhere, so this means all your bullets are facing the same direction. As far as I know, AddForce does not rotate the body at all.

My suggestion is for you to add one more line of code to make the bullet's forward match the player's up direction. And just to be sure the code is working, I'd change the AddForce call to:

 bPrefab.GetComponent<Rigidbody>().AddForce(bPrefab.transform.forward * bulletSpeed);

So you know that the forward vector is pointing where you want it to.

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 JaminEatWorld · Nov 06, 2015 at 09:24 PM 0
Share

Thanks so much for getting back to me - much appreciated.

You seem to understand the issue perfectly. I feel I'm super close to the solution; I need to rotate my bullet prefab in relation to the direction of fire.

When you say "add one more line of code to make the bullet's forward match the player's up direction" - what would the code be? I'm still new to all this and struggling to get it right. Tried a few times without much luck.

$$anonymous$$any thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

help with AI avoidance script 0 Answers

Avoid character bouncing off of obstacles 0 Answers

Unreliability of Physics.Raycast.Distance for player movement? 0 Answers

Avoid character bouncing off of obstacles 1 Answer

Raycast based tile navigation 0 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