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 Sep 20, 2018 at 11:25 PM by polan31 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by polan31 · Apr 28, 2018 at 06:24 PM · script.bouncerotaionfalloff

Why after reflection my bullet falls out of the screen??

I have an object with Physics Material 2D .

My object rotates in four directions after pressing the left mouse button:

-up right

-down right

-down left

-up left

After pressing the right mouse button, my object should firing and bounce off the walls at an angle of 90 degrees in the direction of movement.

I don't understand why after a few reflections object falls out of the screen.

Could someone tell me why my bullet falls out of the screen?

To bounce at 90 degrees after hit I use:

 void OnCollisionEnter2D (Collision2D whatHitMe)
     {
         GameObject g = whatHitMe.gameObject;
            
                 if (g.CompareTag("Background") || g.CompareTag("Enemy"))
                     Debug.Log ("hit");
                 {
                        rb.velocity = Vector2.Reflect(currSpeed, whatHitMe.contacts[0].normal);
                     rb.rotation +=90;    
 
                 
         }
     }
 public void Update ()
 {

     currSpeed = new Vector2(rb.velocity.x, rb.velocity.y);
     }




To shoot the bullet after pressing the right mouse button I use:

 {
 
     if (Input.GetMouseButton(1)) {
         Debug.Log ("Pressed secondary button.");
         rightClicked = true;
         mouseClicked = false;
 
     }
     if(rightClicked)
 
 
         transform.position += (transform.right + transform.up).normalized *  speed * Time.deltaTime;
 
 }
 

For the rotation of the object in four direction I use:

 if (mouseClicked) {        //checks if sprite has already been changed
             Vector3 mousePos = Input.mousePosition;        //gets the current mouse position on screen 
             int currentCase = 0;
             //following does a case-check on the position of your mouse with respect to the sprite:
             if (Camera.main.transform.position.x - (Screen.width/2) + mousePos.x < transform.position.x) {        
                 if (Camera.main.transform.position.x - (Screen.height/2)+mousePos.y < transform.position.y) {
                     currentCase = 2;
                 } else {
                     currentCase = 3;
                 }
             } else {
                 if (Camera.main.transform.position.x - (Screen.height/2)+mousePos.y < transform.position.y) {
                     currentCase = 1;
                 } else {
                     currentCase = 0;
                 }
             }
             //create a new rotation:
             transform.rotation = Quaternion.Euler (0, 0, 90 * currentCase);
         }

PS1: If someone needs a full code, I put it in the attachment.

PS2: I put a video link that shows what the problem is.

 https://gfycat.com/pl/gifs/detail/AjarOffbeatDodobird

PS3: Forgive me that I only put the whole code in the attachment, but some people don't like when the long codes are posted :) :)

link text

I also noticed something:

If I add to the rb.rotation "+" the bullet is reflected only in the right side (which causes stuck or fall out when it should bounce left).

Analogous situation with a "-", but this time bullet reflected only left.

However without "+" or "-" bullet doesn't bounce at all.

t.txt (3.6 kB)
Comment
Add comment · Show 2
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 Captain_Pineapple · Apr 29, 2018 at 09:37 AM 1
Share

Can you reproduce your video with all colliders visible? Just to doublecheck if it actually is a coding problem.

avatar image polan31 Captain_Pineapple · Apr 29, 2018 at 10:11 AM 0
Share

Thank you very much for your answer.

I've been trying to solve this problem for a few days and every help is very nice to me.

I've already edited my post and put new link.

Just in case: I also put a link in the comment:

 https://gfycat.com/pl/gifs/detail/AjarOffbeatDodobird

I'm little disgusted because I tried to solve it with small changes in the code, but nothing helped :) :)

2 Replies

  • Sort: 
avatar image
0

Answer by Serinx · Apr 29, 2018 at 10:06 PM

I've had problems with collision detection when the colliders are too small.

Basically if a collision occurs between 2 physics updates, it will not detect the collision.

e.g. if an object is going really fast, 1 frame its in front of an obstacle, next frame it's on the other side, so the collision isn't detected.

You have 2 options: Increase the Size of the colliders or increase the rate of physics updates.

I'd opt for the former.

It also might help if you don't overlap your colliders

Edit: There is another method where you can use raycasting instead of onCollision.

You basically store the position of the object every frame.

Every frame raycast from the previous position to the current position, if you hit a wall, you have a collision.

Move your object to where the raycast hit, and then perform your reflection code.

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
avatar image
0

Answer by Captain_Pineapple · Apr 30, 2018 at 08:13 AM

Your problem is that you move the collider when you change the rotation of your bullet. Since it is not in the pivot point it will be turned around the transform and change position. In some cases this works, in some others you bullet collider ends up in your walls or behind them. You can solve this problem by making sure that the tip of your bullet (which is also your collider) is always exactly on top of the moving transform of your bullet.

About your second problem: you always turn it in the direction of +90 degrees without regard to the plane you actually hit. (see second 26 of your video, where it turns in the wrong direction) You can try to solve this by a larger if/else tree regarding the normal of the plane you hit, or by choosing the orientation of your bullet with regards to its velocity (let it always look in the direction of your velocity)

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 polan31 · May 01, 2018 at 11:08 AM 0
Share

Hey @Captain_Pineapple, thanks for the answer.

Regarding your first advice:

I placed my circle collider exactly on the top of the bullet, but bullet still crosses the box collider, and after that- stuck in wall.

As for the second advice:

I'm still sitting on it because it's a lot of trouble for me, but if I do something, I'll give it an answer :)

alt text

21.png (211.4 kB)

Follow this Question

Answers Answers and Comments

88 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

Related Questions

Why is gameobject rotating by 90 degrees only once? 1 Answer

Create a scene from script 2 Answers

Open settings dialog like Player, Quality, Render Settings via C#. 0 Answers

Inventory GUI 0 Answers

Object over terrain 2 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