Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Neralm · Mar 03, 2015 at 01:46 AM · c#javascriptscripting problemraycasting

Bullet bounce issue using Vector3.reflect

Hello ,i have gone throught tons of other questions and posts but none of them have helped me figure out a solution for my problem. I apologies for the poor terminology but im fairly new to unity .

The set up of this game is 2D , with camera rotation 0,0,0 . I have made a gun object that is pointing with its ,,green arrow'' towards the mouse point. Then i made it shoot/instantiate bullets , and i made the bullets bounce of the walls using the Vector3.reflect , but i can only get the bullets to bounce properly when they are moving ,,right '' towards Vector3.right . So whenever i instantiate them they are bouncing the way i wanted but instead of starting off towards the mouse point they move 90 degrees to the side towards the ,,red arrow ''

So there are 2 things i've been trying to do:

Either instantiate the bullets with 90 degrees offset on their Z axis ... Or Edit the reflect angle code to work going ,, up '' instead of ,, right '' , but i cant get any of that to work .

Here is the C# scripts that i use for the "gun" object:

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);

 if (Input.GetButtonDown ("Fire1") ) {                                    
         Instantiate(Ball, transform.position , transform.rotation) ;}

And this is the script attached to the ball :

     transform.Translate (Vector3.right * Time.deltaTime * speed);

     Ray ray = new Ray (transform.position, transform.right);
     RaycastHit hit; 
     
 if (Physics.Raycast (ray, out hit, Time.deltaTime * speed + 0.1f, collisionMask)) {
         Vector3 Dir = Vector3.Reflect (ray.direction, hit.normal);
         float rot =    Mathf.Atan2(Dir.y , Dir.x ) * Mathf.Rad2Deg ;
         transform.eulerAngles = new Vector3(0, 0,rot ) ;


I will appreciate any help or advice with this issue as im trying to work it out for the past 4 weeks and its just driving me creazy . Thanks.

Comment
Add comment · Show 6
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 Glurth · Mar 03, 2015 at 01:59 AM 0
Share

I'm confused a bit, can't the ball go in any 2d direction? If so, why are you specifying Vector3.right in your translate function? This implies to me, the ball will only move in that direction.

I would think you would want to specify a movement direction vector for each ball (setting this value based on the angle of the gun when it shot the ball).

avatar image Neralm · Mar 03, 2015 at 02:22 AM 0
Share

well i want the game to be in 2D so yeah id prefer it to only move in 2d direction . This was probably the first thing that worked for me and i just stayed with it. So what you're saying is that i should give a ball a direction based on the "gun"'s rotation , and then change that again on each collision with a wall towards the reflected angle ??

But , when i instantiate it , and give it transform.rotation , isn't that just what you said ? im just a bit confused right now...

avatar image Glurth · Mar 03, 2015 at 02:51 AM 0
Share

For explanation, let's call it "orientation"- since rotation implies movement (to me)

the orientation of the gun, will deter$$anonymous$$e the direction the bullet travels(translation). Is ALSO deter$$anonymous$$es the bullets orientation (not relevant for a ball, but it is for say.. an arrow).

So, you will need to convert that gun's orientation into a Vector2, to define the direction the bullet travels. to Compute the vector you will probably want to use a combination of trig functions (mathf) and Quaternion functions. Let me know if you need help with that.

Once you have the ball moving properly out of the gun, THEN lets address the reflection.

avatar image Neralm · Mar 03, 2015 at 12:43 PM 0
Share

thank you soo much for helping me out with this !

well , when i set the transform.Translate's vector3.right to vector3.UP , the bullet is moving out of the gun properly , but when i do that it does not bounce off the walls properly , basicly i dont know how to rotate/orientate it towards the reflected angle after collision . i belive the problem is that i dont understand the reflection script in 100% ...

But ill try to move the bullet out off the gun with your idea anyway , it seems less complicated then what i've done .

avatar image Neralm · Mar 03, 2015 at 06:19 PM 0
Share

i have no idea how to get it to work using vector2 ... im trying vector2.angle , but i dont think thats correct .

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by studentutu · Feb 22, 2019 at 02:08 PM

@Neralm, If you have Physics material applied to that Bullet (Physically Accurate bullet are not 100% elastic - Bounce value inside Physics Material) you need to change the normal in the hitting surface - (multiple by Bounce value, this is exactly what physics engine doing behind each collision).

Something like this : if (Physics.Raycast(lastLaserPosition, laserDirection, out hit, laserDistance, mask) && hit.transform.gameObject.layer == GameLayer.BORDER_ID) { vertexCounter += 1; distanceToCollider = Vector3.Distance(lastLaserPosition, hit.point); laserPositions.Add(hit.point); lastLaserPosition = hit.point; laserDirection = Vector3.Reflect(laserDirection, hit.normal * bounciness); laserDistance -= distanceToCollider; }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem creating submeshes based on raycast. 0 Answers

Character Clipping Through Walls When Uncrouching and Receiving Errors 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can i move objects around the terrain area and keep the objects to move only in the terrain area ? 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