- Home /
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.
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).
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...
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.
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 .
i have no idea how to get it to work using vector2 ... im trying vector2.angle , but i dont think thats correct .
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; }
Your answer
Follow this Question
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