I have a ball that bounces around the screen off of a paddle. I need to make a guide line when it get close to the paddle showing what direction the ball would bounce off towards. I think the best way to do this is to do a shpere cast around the paddle and when the ball gets in range spawn a line render object as a child of the ball. and set the positions starting with the ball and ending with the paddle. But I don’t know how to get where the ball will hit or how to get where the ball will bounce. Anyone else have any experience with this?
Add component called TrailRenderer to the ball. It will create a line which then disappears over time. Its color, time etc can be tweaked in settings.
EDIT: You may make a function which returns the distance between the ball and the paddle, which then enables TrailRenderer.
Cheers
With particles?
What I need is the opposite of trail renderer I need to show the player where the ball is going to hit the paddle and the path it will follow afterwards.
Sorry for my bad english, I also misunderstood your question.
Gets this code if you want for that.
using UnityEngine;
using System.Collections;
public class guideLine : MonoBehaviour {
public GameObject prefab;
public float length = 15f;
public float step = 0.25f;
public LayerMask layer;
GameObject[] prefabs;
Ray ray = new Ray();
RaycastHit hit = new RaycastHit();
// Use this for initialization
void Start () {
prefabs = new GameObject[Mathf.FloorToInt(length/step)];
for(int i=0;i<prefabs.Length;i++) prefabs[i] = (GameObject)Instantiate(prefab);
}
// Update is called once per frame
void Update () {
transform.Rotate(0f,Input.GetAxis("Horizontal"),0f);
}
void FixedUpdate(){
ray.origin = transform.position;
ray.direction = transform.forward;
int index = 0;
float distance = 0f;
while(index<prefabs.Length){
if(Physics.Raycast(ray,out hit,step,layer)){
distance += Vector3.Distance(ray.origin,hit.point);
if(distance < step){
ray.origin = hit.point;
ray.direction = Vector3.Reflect(ray.direction,hit.normal);
} else {
Physics.Raycast(ray,out hit,length,layer);
prefabs[index].transform.position = ray.origin = ray.origin + ray.direction * Mathf.Abs(step-distance);
if(hit.point == ray.origin) ray.direction = Vector3.Reflect(ray.direction,hit.normal);
index++;
distance = 0f;
}
} else {
Physics.Raycast(ray,out hit,length,layer);
prefabs[index].transform.position = ray.origin = ray.origin + ray.direction * Mathf.Abs(step-distance);
if(hit.point == ray.origin) ray.direction = Vector3.Reflect(ray.direction,hit.normal);
index++;
distance = 0f;
}
}
}
}
Length variable is path total length, and step is separation between path gameobjects.
Download proyect link.
1 Like
Amazing man I love you lol. Thanks a ton.