Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Foldaren · Jul 17, 2020 at 08:56 PM · unity 2dlinerandomizationrandom gen

Trying to draw a line between two randomly generated point

Hello, I'm trying to draw vertical or horizontal lines between point a and point b, and repeat the process until they have reached a final destination. Line length is random. So far, I believe I have managed to make the logic, but I'm hitting a wall now that it comes to actually generating the lines. Note that there aren't any objects at every intersection, the only objects that are present is the "Goal" which is generated inside the screen and the "spawnpoints" which are generated on the screen borders. Here is my code logic. I've tried using linerenderer, but it doesn't seem to be working.

 using UnityEngine;
 
 public class Pathing : MonoBehaviour
 {
     Vector3 newposition, oldposition, goalposition;
     bool changey;
     float targetdistance, newx, newy;
     GameObject goal;
     // Start is called before the first frame update
     void Start()
     {
 ///Initiating variables
         goal = GameObject.Find("Goal(Clone)");//find the object in scene
         goalposition = goal.transform.position;//target to get to
         newposition = transform.position;//initial start point
 ///Determines Directions in X and Y
         if(transform.position.x == -8.4f || transform.position.x == 8.4f)//spawner is horizontal aligned and moves horizontally first
         {
             changey = false;
         }
         if(transform.position.y == -4.5 || transform.position.y == 4.5)//spawner is vertical aligned and moves vertically first
         {
             changey = true;
         }
         while (targetdistance != 0)//as long as distance between the new point to draw the line to and the goal is not 0, continue 
         {
 ///Determines if line directions will be positive or negative
             if(changey == false)//in case of x move turn
             {
                 if (transform.position.x > goalposition.x)
                 {
                     newx = Random.Range(goalposition.x, oldposition.x - 2);//generate x between position x of goal and old generated y position -2, for minimum movement
                     targetdistance = oldposition.x - goalposition.x;
                 }
                 if (transform.position.x < goalposition.x)
                 {
                     newx = Random.Range(oldposition.x + 2, goalposition.x);//generate x between old generated x position +2 and position x of goal, for minimum movement
                     targetdistance = goalposition.x - oldposition.x;
                 }
             }
             if(changey == true)//in case of y move turn
             {
                 if (transform.position.y > goalposition.y)
                 {
                     newy = Random.Range(goalposition.y, oldposition.y - 2);//generate y between position y of goal and old generated y position -2, for minimum movement
                     targetdistance = oldposition.y - goalposition.y;
                 }
                 if (transform.position.y < goalposition.y)
                 {
                     newy = Random.Range(oldposition.y + 2, goalposition.y);//generate y between old generated y position +2 and position y of goal, for minimum movement
                     targetdistance = goalposition.y - oldposition.y;
                 }
             }
 
 
 
 
 
 
 
             oldposition = newposition;//save the previous position
 ///Generate the new target position & the line
             if (changey == false)//horizontal (x)
             {               
                 if (targetdistance < 1f && targetdistance > -1f)//if distance is less than 1
                 {
                     newx = goalposition.x;
                 }
                 newposition = new Vector3(newx, oldposition.y);
                 changey = true;//change direction
                 //Draw the line between oldposition and newposition
 
             }
             if (changey == true)//vertical (y)
             {
                 
                 if (targetdistance < 1f && targetdistance > -1f)//if distance is less than 1
                 {
                     newy = goalposition.y;
                 }
                 newposition = new Vector3(oldposition.x, newy);
                 changey = false;//change direction
                 //Draw the line between oldposition and newposition
             }
         }
     }
 
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

0 Replies

· Add your reply
  • Sort: 

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

211 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 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

Random bitmap generator 0 Answers

How to get a drawn line to snap into a shape? 0 Answers

How to combine two colliders into one 0 Answers

How to make a boss who can shot toward an object, while the boss can do motion like the Earth 1 Answer

,ball flying effect in 2D like 3D 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