-
How to Pass Dotted Ray From Stick to Ball in Billiards Pool Game
about 7 years ago
-
about 7 years ago
Hello Lokesh,
Nice to hear from your side. I checked what you are trying to ask. Below is the code which will help you.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RayHit : MonoBehaviour { public Color c1 = Color.yellow; public Color c2 = Color.red; public int lengthOfLineRenderer = 4; ArrayList<Vector3> linePoints = new ArrayList<Vector3>(); void Start() { LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>(); lineRenderer.material = new Material(Shader.Find("Particles/Additive")); lineRenderer.widthMultiplier = 0.01f; // lineRenderer.numPositions lineRenderer.numPositions = lengthOfLineRenderer; // A simple 2 color gradient with a fixed alpha of 1.0f. float alpha = 1.0f; Gradient gradient = new Gradient(); gradient.SetKeys( new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) } ); lineRenderer.colorGradient = gradient; } void Update() { Ray a = new Ray(transform.position, transform.forward); Ray b; RaycastHit hit; if (Deflect(a, out b, out hit)) { LineRenderer lineRenderer = GetComponent<LineRenderer>(); lineRenderer.SetPosition(0, a.origin); lineRenderer.SetPosition(1, hit.point); lineRenderer.SetPosition(2, (b.origin)); lineRenderer.SetPosition(3, b.origin + 3 * b.direction); } } bool Deflect(Ray ray, out Ray deflected, out RaycastHit hit) { if (Physics.Raycast(ray, out hit)) { Vector3 normal = hit.normal; Vector3 deflect = Vector3.Reflect(ray.direction, normal); deflected = new Ray(hit.point, deflect); return true; } deflected = new Ray(Vector3.zero, Vector3.zero); return false; } }
1 Answer(s)