Easy bullet deflection

Hi, I’ve been looking a way to deflect/reflect/ricochet my bullet for my 2D game and I have searched all the forum in unity and the answers sections and these are not the answers. I want a simpler way that does not need velocity or ray-casting because these two things I’m not good at (Beginner at Unity). What happens is I move the bullet through translation and I want it when it hits anything on trigger (bullet has rigid-body2D and BoxCollider2D) it must reflect/deflect according to it’s angle and the angle of the object (normal) .Can it be done without ray casting and velocity?

if you are working with transform.Translate, then you should look into some tutorials and learn how to use physics, becuase it ignores collision. Raycasting isnt that hard, you have a direction and a distance and thats it.

1 Like

I tried a tutorial about raycasting for bullets but it didn’t work , that why I turned to other solutions. If you have any resources or link I would be grateful!

what tutorial? what wasnt working?

Use raycasting than Vector3.Reflect

He said, that hes not good at raycasting, you could readthe whole post. Also it wouldnt be that realistic because ricoches can happen in weird angles too

https://www.youtube.com/watch?v=iNQB5wPnJrI

@gorbit99 I tried using raycasting with vector3.reflect and the result is that it is not working properly. It works on some angles and directions but not all the times.
Here is the code:

``````using UnityEngine;
using System.Collections;

public class BulletBehavior2D : MonoBehaviour {
public bool isEnabled;
public float speed;
public bool bounceOff;
public float rayLength;
private Ray2D ray;
private RaycastHit2D hit;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

ray = new Ray2D (new Vector2 (transform.position.x, transform.position.y), new Vector2 (transform.right.x, transform.right.y));
RaycastHit2D hit;
Debug.DrawRay (ray.origin, ray.direction * rayLength);
hit = Physics2D.Raycast (ray.origin, ray.direction, rayLength);

if (isEnabled) {
transform.position += transform.right *  Time.deltaTime * speed;
}

}

void OnTriggerEnter2D(Collider2D coll) {

if(bounceOff) {

Vector3 reflect = Vector3.Reflect(transform.right,this.hit.normal);
float rot = 90 - Mathf.Atan2 (reflect.y, reflect.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Inverse(Quaternion.AngleAxis (rot, Vector3.forward));
}

}

}
``````

instead this, use eaycast(…, out hit,…) and put it inside an if statement

``````void OnCollisionEnter2D(Collision2D collide){
movingDir = Vector3.Reflect(movingDir, collide.contacts[0].normal);
}
``````

like i said, moving with transform.translate ignores collision and this may not work

built for a 3d context but this will calculate reflects multiple times and cast a new raycast from the reflecting point. draws a line renderer so you cna see what is up but you can easily remove that part

``````using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class TargetLine : MonoBehaviour {
[Range(1, 5)]
[SerializeField] private int _maxIterations = 3;
[SerializeField] private float _maxDistance = 10f;

private int _count;
private LineRenderer _line;

private void Start() {
_line = GetComponent<LineRenderer>();
}

private void Update() {
_count = 0;
_line.SetVertexCount(1);
_line.SetPosition(0, transform.position);
_line.enabled = RayCast(new Ray(transform.position, transform.forward));
}

private bool RayCast(Ray ray) {
RaycastHit hit;
if (Physics.Raycast(ray, out hit, _maxDistance) && _count <= _maxIterations - 1) {
_count++;
var reflectAngle = Vector3.Reflect(ray.direction, hit.normal);
_line.SetVertexCount(_count + 1);
_line.SetPosition(_count, hit.point);
RayCast(new Ray(hit.point, reflectAngle));
return true;
}
_line.SetVertexCount(_count + 2);
_line.SetPosition(_count + 1, ray.GetPoint(_maxDistance));
return false;
}
}
``````

Still, this isn’t realistic, you should randomize the reflectAngle a bit

he asked for a way to do reflects its up to him to tailor it to suite the context of its use