Line Renderer not rotating with player like other objects?
I am creating a script that creates a trajectory and then fires a projectile along that trajectory, but even though I set lineRenderer.useworldSpace = false, it is still rotating very weirdly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Util_Trajectory : MonoBehaviour
{
public GameObject playerObj;
[SerializeField] List<Vector3> pathVertList = new List<Vector3>();
[SerializeField] float time = 1;
private Vector3 velocity;
[SerializeField] Vector3 acceleration = Vector3.down;
[SerializeField] int splits = 3;
public float Distance_InTime_DueToAcc_InTime(float u, float a, float t)
{
return u* t + 0.5f * a * t * t;
}
public void CalculateTrajectory()
{
if(pathVertList == null)
{
pathVertList = new List<Vector3>();
}
pathVertList.Clear();
float dt = 0;
Vector3 d;
for (int i = 0; i < splits; i++)
{
dt = (time / (splits - 1)) * i;
d.x = Distance_InTime_DueToAcc_InTime(playerObj.transform.forward.x, acceleration.x, dt);
d.y = Distance_InTime_DueToAcc_InTime(playerObj.transform.forward.y, acceleration.y, dt);
d.z = Distance_InTime_DueToAcc_InTime(playerObj.transform.forward.z, acceleration.z, dt);
pathVertList.Add(d);
}
}
public LineRenderer lineRenderer;
[SerializeField] Rigidbody projectile;
private void Awake()
{
lineRenderer.useWorldSpace = false;
}
[Header("Editor Setting")]
[SerializeField] bool calc_Trajectory = false;
[SerializeField] bool auto_calc = false;
[Space]
[SerializeField] bool fire = false;
private void OnDrawGizmosSelected()
{
if(calc_Trajectory || auto_calc)
{
calc_Trajectory = false;
CalculateTrajectory();
lineRenderer.positionCount = splits;
lineRenderer.SetPositions(pathVertList.ToArray());
}
}
public void ThrowBall(Rigidbody rb)
{
rb.velocity = velocity;
}
}
That is my script and here is a video that shows it: https://youtu.be/DtcQwK2dwaE
Answer by streeetwalker · Mar 30, 2020 at 07:11 AM
@DerpyGamerYT , There could be a several reasons why the line does not rotate with the object properly, and confounding the problem is the fact that the code is going through this complicated computation to come up with the points for the line based on playerObject.transform.forward
But, you do not need to do that, because the LineRender is set to draw Relative to it's object's axes. I would suggest you simplify the whole thing:
1. Shoot the line straight out on the z axis, and set each point's x and y to 0.
2. Then rotate the object that contains the lineRender so that it's forward matches your playerObject.forward.
lineRenderObject.transform.rotation = playerObject.transform.rotation;