- Home /
Problem is not reproducible or outdated
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 :) :)
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.
Can you reproduce your video with all colliders visible? Just to doublecheck if it actually is a coding problem.
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 :) :)
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.
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)
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 :)
Follow this Question
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