Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by husbandofemily · Aug 15, 2013 at 07:50 PM · vector3linerendererlocalworld

Vertices Array only given in local coords?

Hi all. I'm trying to make an object that stores it's starting point, moves forward for a given time, then returns and repeats ad infinatum. I've done this with a coUpdate to reset the position, no problem.

However, next, I want the gameobject to make an array of it's vertices, choose one at random, make it the start point for a line renderer, move forward a bit, do the same again for a second random point for the second point in the line renderer, etc, so the effect is a random, wiggly line that zigzags out in one direction, then disappears, and repeats.

I'm almost there under my own steam, but it seems the Vector3 array created for the vertices of the object are in local space. I get the object moving and snapping back to the start, but the linerender shoots along the path of the object instead of staying in world space like I want. Can anyone tell me what I'm doing wrong please? My code:

 var startPos : Vector3;
 var speed = 1.0;
 
 function Start () {
 
    lineRenderer  = gameObject.AddComponent(LineRenderer);
          lineRenderer.material = mat;
          lineRenderer.useWorldSpace= false; 
          lineRenderer.SetWidth(0.2,0.2);
          coUpdate();
          startPos = transform.localPosition;
 }
 
 function FixedUpdate()
     {
     
         transform.Translate(Vector3.forward, Space.Self);
         
     }
 
     
 function coUpdate()
     {
         while(true)
 
     {
         transform.localPosition = startPos;
      var mesh : Mesh = GetComponent(MeshFilter).mesh;
         var vertices : Vector3[] = mesh.vertices;
         for (var i = 0; i < vertices.Length; i++)
         var randNo1 = Random.Range(1,20);
         var point1 = vertices[randNo1];
         
      yield WaitForSeconds (stepTime/3);
         var randNo2 = Random.Range(1,20);
         var point2 = vertices[randNo2];
        // lineRenderer.SetVertexCount(2);
          lineRenderer.SetPosition(0, point1);
          lineRenderer.SetPosition(1, point2);
        yield WaitForSeconds (stepTime/3);
          var randNo3 = Random.Range(1,20);
          var point3 = vertices[randNo3];
          lineRenderer.SetVertexCount(3);
          lineRenderer.SetPosition(2, point3);
          
          
          
         yield new WaitForSeconds(0.2);
         }
     
     }

(excuse the commented out part, I've tried a few different ways!)

I don't expect or want anyone to write the code for me, but I just need a nudge in the right direction...

Cheers

Ian

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 robertbu · Aug 15, 2013 at 08:07 PM

With just a quick read of the code, I suggest that you change to using world coordinates:

 var point1 = transform.TransformPoint(vertices[randNo1]);

You will do the same for 'point2'.

Then for the line renderer you will use world space:

 lineRenderer.useWorldSpace= true; 
Comment
Add comment · Show 3 · 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 husbandofemily · Aug 15, 2013 at 08:27 PM 0
Share

Almost - I get a new type of weirdness now! I changed just the first point to your code, expecting a shooting line with a fixed start. It does this, but the start point is very far away, at a random (but constant) part of the map, definitely not 0,0,0 either. Thanks for the input. I'm currently experimenting with instantiating empties at the points...

avatar image husbandofemily · Aug 15, 2013 at 08:38 PM 0
Share

Ignore me, I was being daft. That works perfectly, thanks!

avatar image robertbu · Aug 15, 2013 at 08:55 PM 0
Share

Actually in running some tests, I was wrong about the local scale.

While you have it solved, I'll post a bit of test script for any future person looking at this issue. It draws a line between two random points in the mesh:

 private var vertices : Vector3[]; 
 private var lineRenderer : LineRenderer;
 
 function Start() {
     vertices = GetComponent($$anonymous$$eshFilter).mesh.vertices;
     lineRenderer = GetComponent(LineRenderer);
     DrawLine();
 }
     
 function Update() {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
         DrawLine();
     }
 }    
     
 function DrawLine() {
     //var point1 = Vector3.Scale(vertices[Random.Range(0,vertices.Length)], transform.localScale);
     var point1 = transform.TransformPoint(vertices[Random.Range(0,vertices.Length)]);
     
     do {
         //var point2 = Vector3.Scale(vertices[Random.Range(0,vertices.Length)], transform.localScale);
         var point2 = transform.TransformPoint(vertices[Random.Range(0,vertices.Length)]);    
     } while (point1 == point2);
     
     lineRenderer.SetPosition(0,point1);
     lineRenderer.SetPosition(1,point2);
 } 

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

15 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

Related Questions

Determining a vector3 at a random point along the local x axis of a gameObject 1 Answer

Finding Position based on a local perspective 1 Answer

How to move the character relative to the camera? 0 Answers

rotate vector around local axis 2 Answers

Accessing local system ( File Browser ) 2 Answers


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