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 AyeEmTeeBee131 · Aug 26, 2019 at 01:19 AM · raycastbulletprojectilereflectionreflect

Bullet reflect not working properly

I'm trying to have a bullet projectile using raycast as collider to bounce off certain reflective objects. From what I can tell, it doesn't seem to mainly reflect based on where the bullet hits the object, but based on the reflective object's rotation. If the reflective object's rotation is 0, then no matter where the bullet hits it from, it will reverse its speed like it's hitting the object straight on.

I'm assuming the code I have does not take the rotation of both bullet and object into account. What can I change to make this work? Thanks in advance.

Code below here:

using UnityEngine; public class BulletBounce : MonoBehaviour { public float time; public float speed;

 private Vector3 direction;

 void Awake()
 {
     direction = Vector3.forward;
 }

 void FixedUpdate()
 {
     RaycastHit hit;
     if (Physics.Raycast(transform.position, transform.TransformDirection(direction), out hit, speed))
     {
         if (hit.transform.tag != "Player" && hit.transform.tag != "IgnoreBullet")
         {

             if (hit.transform.tag == "Reflect")
             {
                 direction = Vector3.Reflect(direction, hit.normal);
                 transform.position = hit.point;
                 transform.rotation.SetFromToRotation(Vector3.forward, direction);
                 return;
             }
         }
     }
     transform.Translate(direction * speed);
 }

}

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
Best Answer

Answer by Namey5 · Aug 26, 2019 at 03:35 AM

transform.Translate works in object space, but your direction is in world space. Replace the last line with this;

 transform.position += direction * speed;
Comment
Add comment · Show 3 · 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 AyeEmTeeBee131 · Aug 26, 2019 at 05:27 PM 0
Share

By doing that, it seems that the bullet's movement does not take it's own rotation into account, as it only initially moves forward on the world z-axis. Is there a potential way to have it both move on it's own z-axis while being able to reflect off of objects?

avatar image Namey5 AyeEmTeeBee131 · Aug 27, 2019 at 01:56 AM 0
Share

Sorry, didn't realise you also initialised it to a local space direction. In your awake function, replace the line with this;

 direction = transform.forward;

It should start in the right direction now.

avatar image AyeEmTeeBee131 Namey5 · Aug 27, 2019 at 06:16 PM 0
Share

After changing the line "if (Physics.Raycast(transform.position, transform.TransformDirection(direction), out hit, speed))" to "if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, speed))", I think I've got it working for now.

$$anonymous$$any thanks for your help!

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

157 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

Related Questions

Laser LineRenderer Tapers after first Reflect 0 Answers

Fire Raycast Toward Current Mouse Position 1 Answer

Best way to shoot physical bullets? 2 Answers

How to rotate a projectile and mantain constant speed when hitting a wall? 2D 0 Answers

Raycasr in my fps? 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