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;