1
votes

im kinda new to unity and coding so im sorry for silly mistakes and not using proper language to describe things.

Im trying to make a 2d shooting game where the bullet bounces off the walls like a ricochet . I created a Ray pointing to the direction the bullet is going and then tested if that ray collides with the walls , i had to create a 3d box colliders where the walls are, because ray wouldn't detect 2d collider . Now im trying to use Vector3.reflect to change the rotation of the bullet so it would bounce off like a ricochet but instead its bouncing to random directions and sometimes its going through the box colliders. I just would like to know where is the problem , is it in the code or the scene setup ? if anybody has some advice,tip or even criticism i would appreciate .thanks

here is the code attached to the bullet void Update () {

    Vector3 fwd = transform.TransformDirection(Vector3.up);
    //Debug.DrawRay(raycastObject.transform.position, fwd * raySize, Color.green);

    RaycastHit hit;
    Ray ray = new Ray (transform.position , fwd * raySize ) ;
    Debug.DrawRay (transform.position, fwd * raySize); 

    if (Physics.Raycast (ray , out hit, raySize  )) {
        Debug.Log ("Bouce test 1 ");

                    if (hit.collider.tag == "Wall") {
                            Debug.Log ("Bounce test 2 ");

                            Vector3 reflectDir = Vector3.Reflect (ray.direction , hit.normal);
                            float rot =  Mathf.Atan2 (reflectDir.y, reflectDir.x) * Mathf.Rad2Deg;
                            transform.eulerAngles = new Vector3 (0, 0, rot);

                }

and this is code for movement of the bullet

       Vector3 pos = transform.position; 
       Vector3 velocity = new Vector3 (0, speed * Time.deltaTime, 0); 
       pos += transform.rotation * velocity;
       transform.position = pos; 
3

3 Answers

1
votes

It looks to me like your issues has to do with the fact that this is a really round about way to go about things where you are working in 3D space, and yet using 2D spacial functions to calculate your transformation, and then finally preforming your rotation in only one dimension (one dimension as far as polar coordinates go, obviously 2 in Cartesian space). For related reasons you already have found that you needed to use the 3D box colliders instead of their 2D cousins to get the results you want. When building with 3D objects, try to stick with 3D functions, and other 3D things. Mixing dimensions is going to make things painfully complicated and will likely result in difficult to read code and problems that become difficult to solve.

Your solution really depends on what you are trying to do with your game. In a fully 3D game piping spacial calculations through various 2D methods is just going to be a recipe for disaster. For an effectively 2D game made up of 3D objects it will still be simpler to program reflecting your Vector using the 3D functions, and if necessary due to 3D geometry sloping out of the field of play, then projecting the result into the 2D plane of play and then normalizing and/or scaling it as necessary.

Assuming no complicating geometry, you should be able to replace this:

Vector3 reflectDir = Vector3.Reflect (ray.direction , hit.normal);
float rot =  Mathf.Atan2 (reflectDir.y, reflectDir.x) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3 (0, 0, rot);

with this:

Vector3 reflectDir = Vector3.Reflect (ray.direction , hit.normal);
transform.rotation.SetFromToRotation(ray.direction, reflectDir);

Give that a try. If you need to project things in plane afterwards due to sloping 3D geometry let me know, and I'll try to hack something together to fix it up for you.

1
votes

Again in the Unity 2D tutorials, this looks like a great overview of exactly the type of effects you might need to work with:

https://unity3d.com/learn/tutorials/modules/beginner/2d/sliding-bouncing-2d?playlist=17093

You would have to start over, but it looks like "bounce" is included in the 2D collider, so you literally would just have to turn that feature on and turn up the bounce to get the ricochet you want. Again though, I haven't worked with Unity 2D myself, so I could be wrong about that.

0
votes

"Im trying to make a 2d shooting game where the bullet bounces off the walls like a ricochet."

Also if you want this to be strictly 2D, you can greatly simplify your life by using the various Unity 2D objects, functions, and variables, like Vector2 instead of Vector3, at which point things like the 2D colliders you were trying to use should be usable (and the 3D box colliders you switched to won't).

I am not great with Unity, and haven't played with Unity2D at all, but from the sound of what you are trying to do Unity2D will make your life a lot easier. You might want to look into its documentation and tutorials here:

https://unity3d.com/learn/tutorials/topics/2d-game-creation

Trying to do 2D stuff with fully 3D tools/variables/functions is just a needless extra layer of complexity. It would be like trying to fly just barely above the ground along a road through a complex and crowded city in a jet instead of just getting a car and driving instead. If you don't rid of a dimension you don't need it will just make things more complex.