Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DaiMangouDev · Apr 23, 2016 at 06:24 PM · c#transformlinerendererpositioning

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);

     

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaiMangouDev · Apr 24, 2016 at 11:10 PM 0
Share

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 ?

avatar image Soraphis DaiMangouDev · Apr 25, 2016 at 09:41 AM 0
Share

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

avatar image DaiMangouDev Soraphis · Apr 25, 2016 at 10:07 AM 0
Share

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

avatar image DaiMangouDev · Apr 25, 2016 at 11:02 AM 0
Share

Just one thing, When the sphere is scaled , the line scales with it =/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges