'Shoot raycast on reflect direction
I'm creating a pool game. And I want to shoot Raycast
on the reflect direction so I can draw LineRenderer
on it.
Main Cue Ball Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawCueBallTrajectory : MonoBehaviour
{
private RaycastHit hit;
private LineRenderer lineRender;
private float cueBallRadius;
private void Awake()
{
lineRender = GetComponent<LineRenderer>();
cueBallRadius = GetComponent<SphereCollider>().radius;
}
void Update()
{
Vector3 reduceTransPos = new Vector3(transform.position.x, transform.position.y - 2f, transform.position.z);
Ray ray = new Ray(transform.position, -transform.forward);
if (Physics.SphereCast(ray, cueBallRadius, out hit))
{
if (hit.collider != null)
{
lineRender.enabled = true;
lineRender.SetPosition(0, transform.position);
lineRender.SetPosition(1, -transform.forward + new Vector3(hit.point.x, hit.point.y + 1f, hit.point.z));
if (hit.collider.gameObject.CompareTag("OtherCueBall"))
{
Vector3 newDirection = Vector3.Reflect(hit.transform.position, hit.normal);
hit.collider.gameObject.GetComponent<OtherCueBallTrajectory>().DrawPredictionLine(newDirection);
}
}
}
}
}
Other Ball Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OtherCueBallTrajectory : MonoBehaviour
{
private RaycastHit hit;
private LineRenderer lineRender;
private float cueBallRadius;
private void Awake()
{
lineRender = GetComponent<LineRenderer>();
cueBallRadius = GetComponent<SphereCollider>().radius;
}
public void DrawPredictionLine(Vector3 targetDestination)
{
Ray ray = new Ray(transform.position); // what to put here
}
}
Solution 1:[1]
You must emit another sphere cast along the point of impact. Also adjust the lineRender
points count according to the number of fractures. I made these changes to the script main cue ball
and do not see the need to refer the second sphere cast to the other ball
.
if (Physics.SphereCast(ray, cueBallRadius, out hit))
{
if (hit.collider != null)
{
lineRender.enabled = true;
lineRender.positionCount = 2; // here I set lineRender position count
lineRender.SetPosition(0, transform.position);
lineRender.SetPosition(1, -transform.forward + new Vector3(hit.point.x, hit.point.y + 1f, hit.point.z));
if (hit.collider.gameObject.CompareTag("OtherCueBall"))
{
Vector3 newDirection = Vector3.Reflect(hit.transform.position, hit.normal);
lineRender.positionCount = 3; // we need 1 more break point
// second sphere cast begin from hit.point along reflectDirection
if (Physics.SphereCast(hit.point, cueBallRadius, newDirection, out var secondHit))
{
lineRender.SetPosition(2, secondHit.point); // stop when hit wall or anything
}
else
{
lineRender.SetPosition(2, hit.point + newDirection * 10f); // point along direction for maximum of 10f length for e.g..
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |