• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Random Raycast inside crosshairs 1 Answer

Bullet collision for space shooter - colliders, rays, etc? 2 Answers

Handling Projectiles in a Multiplayer Environment 0 Answers

Simulating gravity to Raycast bullet 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges