How do I draw a local space line render between parent and child objects ?
Hi , I really need some help here
I'm just trying o draw a lical space line renderer between the pare and child objects , but it is just not drawing the line correctly no matter what i try.
here is my script
To use this script , you must
1 create a cube and a sphere.
2 make the sphere a child of the cube.
3 add the script to the cube.
4 set the position of the sphere to (0,3,0)
public class AddLine : MonoBehaviour {
public Transform Ball;
void Start()
{
LineRenderer line = Ball.gameObject.AddComponent<LineRenderer>();
line.SetVertexCount(2);
line.SetWidth(0.015F, 0.015F);
// we want the lines to use local space and not world space
line.useWorldSpace = false;
line.useLightProbes = false;
line.receiveShadows = false;
line.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
line.material.color = Color.red;
}
void FixedUpdate ()
{
// begin the line at the cubes position
Vector3 Start = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
// to that the line ends at the center of the ball
Vector3 End = new Vector3();
//Set the begin and the end of the line renderer
Ball.GetComponent<LineRenderer>().SetPosition(0, Start);
Ball.GetComponent<LineRenderer>().SetPosition(1, End);
}
}
Answer by Soraphis · Apr 24, 2016 at 08:02 PM
About Fixed Update, check this out: (1) or this (2)
you should use Update instead.
to get your starting point (in local space) use: Vector 3 Start = Vector3.zero;
which is actually your end point :P
to get your end point use: Vector 3 End = Ball.position - transform.position
its possible that this will not work if you use some kind of scaling or rotating, so its more safe if you use:
Vector3 End = transform.worldToLocalMatrix.MultiplyPoint(Ball.position);
edit: since your Ball is a child of the Cube you can simply use:
Vector3 End = Ball.localPosition;
if you would use a world space line renderer you could simply use:
Vector3 Start = transform.position;
Vector3 End = Ball.position;
Thanks a lot , this is getting me somewhere . awesome. +5 + 1 up One more thing :D
move your sphere on the x or z axis by 3.
how do we then move the line so that it is drawn at the x and z position of the ball ins$$anonymous$$d of the cubes x and z position ?
you mean kind of a ray going straight down from the ball? is 3 an example or a magic number (can it be more than 3)?
well, you could store a Vector3 BallStartPosition
into your script which is filled in your start method by: BallStartPosition = Ball.position
in your update method check if the if ( (Ball.position - BallStartPosition).magnitude > 3)
now you know your Ball has moved more than 3 from its starting position. and you have to change the Start vectory by: Start.x = End.x
and Start.z = End.z
I just use
Vector3 End = new Vector3
(0,
transform.worldToLocal$$anonymous$$atrix.$$anonymous$$ultiplyPoint(Ball.position).y,
0
);
many thanks . you really really helped me out here
Just one thing, When the sphere is scaled , the line scales with it =/
Your answer
Follow this Question
Related Questions
Calculate a point in space based on a triangle 0 Answers
Position Child Object to center of Parent Object [SOLVED] 1 Answer
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers
Problems With .rotate behavior 1 Answer