- Home /
Block Braker with Raycasts
Hey guys,
Im making a game similar to block breaker but as my walls are modular, i cant use Unity physics as it keeps hitting the collider corners on each wall piece. The only real way around this is to gut the physics and use raycasts which normalize the contact point so the walls are more uniformed as one surface and not hitting the individual physics colliders.
Has any one got any thoughts on how to code the ball using a raycast + vector3.reflect? Ive had a few attempts with no luck so far.
Answer by highpockets · Apr 18, 2019 at 06:42 AM
Assuming that you already know the direction the ball is going in, here:
Vector3 ballDirection;
RaycastHit hit;
Vector3 newBallDirection;
if(Physics.Raycast(ball.transform.position, ballDirection, our hit, Mathf.Infinity)){
newBallDirection = Vector3.Reflect(ballDirection, hit.normal);
}
The above, works off the ball’s original direction direction (before hitting the wall), it gets the raycast hit normal and returns the reflected direction. Hope that helps
@highpockets thanks for you help with this so far. Ive made some progress but the "Ball" is bouncing 180 and not taking into account any angles at all. Any further help would be much appreciated!
public float bulletVel = 17f;
public Vector3 bulletDir;
public Vector3 newBulletDir;
private void Awake()
{
//Starting directional movement
bulletDir = Vector3.right;
}
private void Update()
{
RaycastHit hit;
if (Physics.Raycast(this.transform.position, bulletDir, out hit, $$anonymous$$athf.Infinity))
{
newBulletDir = Vector3.Reflect(bulletDir, hit.normal);
}
}
private void OnCollisionEnter(Collision collision)
{
bulletDir = newBulletDir;
}
private void FixedUpdate()
{
transform.Translate(bulletDir * Time.deltaTime * bulletVel);
}
You are getting the starting directional movement as the positive x in world space, are you sure that is the correct direction. I’d imagine you would want to get a direction vector from the object itself. If it travels along its local right/red axis, then you would use transform.right , not Vector3.right.. on the other hand, it’s completely possible that you shoot in the world positive x direction, just seems a bit strange
I think im pretty much going to give in on this one. Ive spent the last three days looks at this. Im just not getting any closer, just further away it seems. :/
public float bulletVel = 4f;
public Vector3 bulletDir;
public Vector3 newBulletDir;
public Layer$$anonymous$$ask layer$$anonymous$$ask;
public Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
bulletDir = (rb.velocity = this.transform.forward * bulletVel);
}
private void Update()
{
transform.Translate(transform.forward * Time.deltaTime * bulletVel);
//cast a gizmo ray to show direction
Ray ray = new Ray(transform.position, gameObject.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.green);
//Reflect Attempts
RaycastHit hit;
if (Physics.Raycast(bullet.transform.position, bulletDir, out hit, $$anonymous$$athf.Infinity, layer$$anonymous$$ask))
{
newBulletDir = Vector3.Reflect(bulletDir, hit.normal);
}
}
private void OnCollisionEnter(Collision collision)
{
//Update new direction
Debug.Log("Dir updated from " + bulletDir + "to " + newBulletDir);
bulletDir = newBulletDir;
}