Search Unity

How to make a line render show a bounce?

Discussion in 'Scripting' started by ardizzle, Sep 1, 2014.

  1. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    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?
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    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 :)
     
  3. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    With particles?
     
  4. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    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.
     
  5. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Sorry for my bad english, I also misunderstood your question.

    Gets this code if you want for that.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class guideLine : MonoBehaviour {
    5.  
    6.     public GameObject prefab;
    7.     public float length = 15f;
    8.     public float step = 0.25f;
    9.     public LayerMask layer;
    10.  
    11.     GameObject[] prefabs;
    12.     Ray ray = new Ray();
    13.     RaycastHit hit = new RaycastHit();
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.  
    18.         prefabs = new GameObject[Mathf.FloorToInt(length/step)];
    19.  
    20.         for(int i=0;i<prefabs.Length;i++) prefabs[i] = (GameObject)Instantiate(prefab);
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         transform.Rotate(0f,Input.GetAxis("Horizontal"),0f);
    28.    
    29.     }
    30.  
    31.  
    32.     void FixedUpdate(){
    33.  
    34.         ray.origin = transform.position;
    35.         ray.direction = transform.forward;
    36.  
    37.         int index = 0;
    38.         float distance = 0f;
    39.  
    40.         while(index<prefabs.Length){
    41.  
    42.  
    43.             if(Physics.Raycast(ray,out hit,step,layer)){
    44.  
    45.                 distance += Vector3.Distance(ray.origin,hit.point);
    46.  
    47.                 if(distance < step){
    48.  
    49.                     ray.origin = hit.point;
    50.                     ray.direction = Vector3.Reflect(ray.direction,hit.normal);
    51.  
    52.                 } else {
    53.  
    54.                     Physics.Raycast(ray,out hit,length,layer);
    55.                     prefabs[index].transform.position = ray.origin = ray.origin + ray.direction * Mathf.Abs(step-distance);
    56.                     if(hit.point == ray.origin) ray.direction = Vector3.Reflect(ray.direction,hit.normal);
    57.                     index++;
    58.                     distance = 0f;
    59.  
    60.                 }
    61.  
    62.             } else {
    63.  
    64.                 Physics.Raycast(ray,out hit,length,layer);
    65.                 prefabs[index].transform.position = ray.origin = ray.origin + ray.direction * Mathf.Abs(step-distance);
    66.                 if(hit.point == ray.origin) ray.direction = Vector3.Reflect(ray.direction,hit.normal);
    67.                 index++;
    68.                 distance = 0f;
    69.    
    70.             }
    71.  
    72.         }
    73.  
    74.     }
    75.  
    76. }
    77.  
    Length variable is path total length, and step is separation between path gameobjects.

    Download proyect link.
     
    ardizzle likes this.
  6. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Amazing man I love you lol. Thanks a ton.