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 /
This question was closed Jun 05, 2018 at 03:32 PM by MilkyTheCarton for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by MilkyTheCarton · May 11, 2018 at 04:30 PM · collider2d2d-physicslinerendererbounceraycasthit2d

How can i shoot 2D ball/bounce of wall using Raycast?

Hello fellow traveler, i am trying to shoot a ball and be able to bounce of wall and keep going like in Puzzle Bubble/Pool games.


alt text
My shooting code, Cannon.cs:

 void Update () {
 
         // Raycast code
         if( fireRate == 0)
         {
             if (Input.GetButtonDown("Fire1"))
             {
                 Shoot();
             }
         }
         else
         {
             if (Input.GetButton("Fire1") && Time.time> timeToFire)
             {
                 timeToFire = Time.time + 1 / fireRate;
                 Shoot();
             }
         } 
     }
 
     //Raycast code
     void Shoot()
     {
         Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
         Vector2 firePointPosition = new Vector2(bullet.position.x, bullet.position.y);
         Vector2 val = mousePosition - firePointPosition;
         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, val, 100, notToHit);
         Effect();
         Debug.DrawLine(firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
         if(hit.collider != null)
         {
             Debug.DrawLine(firePointPosition, hit.point, Color.red);
             //Debug.Log("We hit " + hit.collider.name);
             if(hit.collider.GetComponent<BoxInteraction>() != null)
             {
                     hit.collider.GetComponent<BoxInteraction>().beenHit();
             }
         }
     }


With the current code i am able to hit the boxes, i use the Debug.DrawLine for testing, check what i'm hitting and whatnot.
Now i am stuck at how i can simulate shooting 1 ball at a time and it being able to bounce of the wall and creating an aim line so i can know where it will hit before shooting like in Pool games.
alt text
Can it be done using Raycast? If yes, how?
Any nudge in the right direction its very much appreciated.

~Milky

4.png (62.1 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

1 Reply

  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · May 11, 2018 at 07:01 PM

Good day.

The best way is Instantiate a ball with a collider, and detect with what element is the collision with

  OnColliderEnter (Collision TheOtherObject)
 {
 }

bye!

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 MilkyTheCarton · May 11, 2018 at 07:21 PM 0
Share

Okay, if i use this method for going at it then how can i make the ball bounce of the wall and hit a specific point where i am ai$$anonymous$$g? Also this doesn't answer another part of my original question, how can i use Raycast to simulate ai$$anonymous$$g line, like Puzzle Bubble/Pool table games?

Follow this Question

Answers Answers and Comments

85 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

Related Questions

How to refract a linerenderer through a 2d object using Physics2D ? 0 Answers

When an object bounces between two bouncy objects it goes through them 1 Answer

Adjusting an angle of a collider2D to a linerenderer that is user-created 0 Answers

Unity 2D -Reflecting a linerenderer 2 Answers

HOW TO ONLY DETECT ANGLES BY COLLIDERS2D FACES 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