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 VinHill · Apr 18, 2019 at 04:35 AM · raycastraycastingballbouncebounces

Block Braker with Raycasts

Hey guys,

Im making a game similar to block breaker but as my walls are modular, i cant use Unity physics as it keeps hitting the collider corners on each wall piece. The only real way around this is to gut the physics and use raycasts which normalize the contact point so the walls are more uniformed as one surface and not hitting the individual physics colliders.

Has any one got any thoughts on how to code the ball using a raycast + vector3.reflect? Ive had a few attempts with no luck so far.

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

Answer by highpockets · Apr 18, 2019 at 06:42 AM

Assuming that you already know the direction the ball is going in, here:

 Vector3 ballDirection;
 RaycastHit hit;
 Vector3 newBallDirection;
 
 if(Physics.Raycast(ball.transform.position, ballDirection, our hit, Mathf.Infinity)){
 
 newBallDirection = Vector3.Reflect(ballDirection, hit.normal);
 }

The above, works off the ball’s original direction direction (before hitting the wall), it gets the raycast hit normal and returns the reflected direction. Hope that helps

Comment
Add comment · Show 5 · 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 VinHill · Apr 19, 2019 at 03:37 AM 0
Share

@highpockets thanks for you help with this so far. Ive made some progress but the "Ball" is bouncing 180 and not taking into account any angles at all. Any further help would be much appreciated!

     public float bulletVel = 17f;
     public Vector3 bulletDir;
     public Vector3 newBulletDir;
    
 
     private void Awake()
     {
         //Starting directional movement
         bulletDir = Vector3.right;
     }
 
     private void Update()
     {
         
         RaycastHit hit;
 
         if (Physics.Raycast(this.transform.position, bulletDir, out hit, $$anonymous$$athf.Infinity))
         {
             newBulletDir = Vector3.Reflect(bulletDir, hit.normal);
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         bulletDir = newBulletDir;
     }
     private void FixedUpdate()
     {
         transform.Translate(bulletDir * Time.deltaTime * bulletVel);
     }
avatar image highpockets VinHill · Apr 19, 2019 at 10:21 AM 0
Share

You are getting the starting directional movement as the positive x in world space, are you sure that is the correct direction. I’d imagine you would want to get a direction vector from the object itself. If it travels along its local right/red axis, then you would use transform.right , not Vector3.right.. on the other hand, it’s completely possible that you shoot in the world positive x direction, just seems a bit strange

avatar image VinHill highpockets · Apr 21, 2019 at 02:08 AM 0
Share

I think im pretty much going to give in on this one. Ive spent the last three days looks at this. Im just not getting any closer, just further away it seems. :/

     public float bulletVel = 4f;
     public Vector3 bulletDir;
     public Vector3 newBulletDir;
     public Layer$$anonymous$$ask layer$$anonymous$$ask;
 
     public Rigidbody rb;
 
 
     private void Awake()
     {
         rb = GetComponent<Rigidbody>();
         bulletDir = (rb.velocity = this.transform.forward * bulletVel);
         
     }
 
     private void Update()
     {
         transform.Translate(transform.forward * Time.deltaTime * bulletVel);
 
 
         //cast a gizmo ray to show direction
         Ray ray = new Ray(transform.position, gameObject.transform.forward);
         Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.green);
 
         //Reflect Attempts
         RaycastHit hit;
 
         if (Physics.Raycast(bullet.transform.position, bulletDir, out hit, $$anonymous$$athf.Infinity, layer$$anonymous$$ask))
         {
             newBulletDir = Vector3.Reflect(bulletDir, hit.normal);
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         //Update new direction
         Debug.Log("Dir updated from " + bulletDir + "to " + newBulletDir);
         bulletDir = newBulletDir;
         
     }

Show more comments

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

153 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

Related Questions

SImple bouncing ball with pysics : wierd and not logical bounce 0 Answers

Raycast script help? 1 Answer

raycast is slowing down computer... 2 Answers

Can't find a clone object with RayCast? [Solved] 2 Answers

How can I differentiate between colliding with an object or its child? 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