Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast.point to lineRenderer position is off

Discussion in 'Editor & General Support' started by h0nka, Jul 14, 2014.

  1. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Hey guys. I'm having a problem with setting the lineRenderer.setPosition(1, hit.point) from a raycast that goes from the mouse position directly down to the ground. The raycast does hit the ground directly below the mouse, but when i set the line renderer position to hit.point, it points way off to the right and not where the raycast hits the ground. Does anyone have any ideas?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DashLine : MonoBehaviour {
    5.    
    6.     private LineRenderer lineRenderer;
    7.     private JumpBoost jumpBoost;
    8.     private float distance;
    9.     private GameObject player;
    10.    
    11.     // Use this for initialization
    12.     void Awake () {
    13.    
    14.         lineRenderer = gameObject.GetComponent<LineRenderer> ();
    15.         lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
    16.         lineRenderer.SetColors (Color.cyan, Color.cyan);
    17.         lineRenderer.SetWidth(0.02F, 0.02F);
    18.         lineRenderer.enabled = false;
    19.  
    20.         player = GameObject.FindGameObjectWithTag("Player");
    21.         jumpBoost = player.GetComponent<JumpBoost> ();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         if(Input.GetButton("Fire2"))
    28.         {
    29.             Time.timeScale = 0.5f;
    30.  
    31.             Vector3 worldMousePosition =  Camera.main.ScreenToWorldPoint (Input.mousePosition);
    32.             worldMousePosition = new Vector3 (worldMousePosition.x, worldMousePosition.y, 0f);
    33.             Vector3 playerPos = player.transform.position;
    34.             Vector3 direction = worldMousePosition - player.transform.position;
    35.  
    36.             Vector2 worldMousePosition2D = new Vector2 (worldMousePosition.x, worldMousePosition.y);
    37.  
    38.             if(direction.x > 3f || direction.x < -3f)
    39.             {
    40.                 direction.x = Mathf.Clamp(direction.x, -3f, 3f);
    41.             }
    42.  
    43.             RaycastHit2D hit = Physics2D.Raycast (worldMousePosition2D, -Vector2.up * 10);
    44.  
    45.             lineRenderer.SetPosition (0, new Vector3 (direction.x, direction.y, 0f));
    46.             lineRenderer.SetPosition (1, hit.point);
    47.             lineRenderer.enabled = true;
    48.  
    49.             Debug.DrawRay(worldMousePosition2D, -Vector2.up * 10);
    50.         }
    51.         else
    52.         {
    53.             lineRenderer.enabled = false;
    54.             Time.timeScale = 1f;
    55.         }
    56.     }
    57. }
    58.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    LineRenderer uses local space, so that would only work if the LineRenderer's position is (0,0,0), rotation is (0,0,0), and scale is (1,1,1). If it is a child to anything else, the parents must have the same values.
     
    h0nka likes this.
  3. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Thanks for the reply. The problem is if i set the position to (0,0,0), then the:
    lineRenderer.SetPosition(0, newVector3(direction.x, direction.y, 0f));
    is off to the left of the player object. Is there a way around that problem or maybe another approach to getting the same effect?

    I dunno how clear it is, but i am trying to draw a line from the mouse to nearest ground relative to the player object but to a limited x distance from the player (which is the length the player will be transportet to when "fire2" is released, in another script).
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Try
    Code (csharp):
    1.  
    2. lineRenderer.SetPosition(0, player.transform.TransformPoint(direction.x, direction.y, 0f));
     
    h0nka likes this.
  5. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    This worked pretty great, but only when the player is facing the right, due to the x-scale becoming -1 when facing left. Could i use mathf.abs?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    player.transform.position + player.transform.rotation * new Vector3(direction.x, direciton.y, 0f)
     
    h0nka likes this.
  7. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    One last question if you don't mind (kinda been going insane over this script the last few days), you know to get the last position
    Code (CSharp):
    1. lineRenderer.SetPosition (1, hit.point);
    not exceed the x-value of the first position so the line will always be a straight vertical line?
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    I don't follow?
     
  9. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Well the linerenderer had too many complications, so i've made a prefab which follows the hit.point on the ground. I seem to have the same problem however as before. The prefab seems to be offset to the left of the player (i'm guessing it is in world space then) and i can't seem to convert the prefab position to local space relative to the player object.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DashLine : MonoBehaviour {
    5.  
    6.     private JumpBoost jumpBoost;
    7.     private GameObject player;
    8.     private GameObject arrowPrefab;
    9.     private float distance;
    10.  
    11.     public GameObject arrow;
    12.  
    13.     // Use this for initialization
    14.     void Awake () {
    15.  
    16.         player = GameObject.FindGameObjectWithTag("Player");
    17.         jumpBoost = player.GetComponent<JumpBoost> ();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         Vector3 worldMousePosition =  Camera.main.ScreenToWorldPoint (Input.mousePosition);
    24.         worldMousePosition = new Vector3 (worldMousePosition.x, worldMousePosition.y, 0f);
    25.         Vector2 worldMousePosition2D = new Vector2 (worldMousePosition.x, worldMousePosition.y);
    26.  
    27.         RaycastHit2D hit = Physics2D.Raycast (worldMousePosition2D, -Vector2.up * 10);
    28.  
    29.         if(Input.GetButtonDown("Fire2"))
    30.         {
    31.             if(hit.collider)
    32.             {
    33.                 arrowPrefab = (GameObject) Instantiate(arrow, hit.point, Quaternion.identity);
    34.             }
    35.         }
    36.  
    37.         if(Input.GetButton("Fire2"))
    38.         {
    39.             Time.timeScale = 0.5f;
    40.  
    41.             if(hit.collider != null)
    42.             {
    43.                 distance = hit.point.x - player.transform.position.x;
    44.                 distance = Mathf.Clamp(distance, -jumpBoost.boostLength, jumpBoost.boostLength);
    45.                 Debug.Log (distance);
    46.             }
    47.  
    48.             Vector2 temp = arrowPrefab.transform.position;
    49.             temp.x = distance;
    50.             arrowPrefab.transform.position = temp;
    51.  
    52.             Debug.DrawRay(worldMousePosition2D, -Vector2.up * 10);
    53.         }
    54.         else
    55.         {
    56.             Time.timeScale = 1f;
    57.             Destroy (arrowPrefab);
    58.         }
    59.  
    60.  
    61.     }
    62. }
    63.  
     
  10. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Ok so i fixed that by making the prefab a parent of the player object and using localPosition, but again i get the problem when facing left the prefab moves opposite...
     
  11. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Never mind. It used your earlier solution to the problem which fixed it. Again, thanks for help!