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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by cocleu · Sep 15, 2013 at 02:54 AM · linerendererpointpathfollowingdrawline

Draw line in runtime

Hello everyone, Iam working in project that require player point some points on ground (or input cordinates of points) to draw lines between points then press go and my gameobject will move along that path (lines still exist when gameObject moving). How can i do that. Thanks alot.

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
4
Best Answer

Answer by vexe · Sep 15, 2013 at 03:51 AM

For drawing lines, check out the line renderer, here's some tuts: tut1, tut2.

You should have your points already in an array or a list. Draw the lines you want between them, and have your object move through the points one by one, use a Lerping function to smooth your movement. Something like:

 IEnumerator MoveTo (Transform objectToMove, Vector3 targetPosition, float timeToTake)
 {
     float t = 0;
     Vector3 originalPosition = objectToMove.position;
     while (t < 1)
     {
         // putting the increment of t before the lerp, will make it so that by the end of the loop our object would reach 
         // its target position with 100% because t is clampped between 0 to 1 in the lerp.
         // but if we but the increment after it - in the last run - t would become something like 0.99942 or something
         // we'd lerp to that - and then t would go beyond 1 and the loop would exit - and we'd end up with our object sitting at 0.99942
         // of the road - not 1
         t += Time.deltaTime / timeToTake;
         objectToMove.position = Vector3.Lerp (originalPosition, targetPosition, t);
         yield return null;
     }
 }

Here's a very quick and dirty implementation (attach this to the object you're moving):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class MoveToPoints : MonoBehaviour
 {

   // Inspector variables, pre-set by you
   public List<Transform> locations;
   public GameObject trail;           // instead of drawing lines, I'm gonna instantiate a trail object along the way
   public float timeToTake = 1f;

   private Transform _transform;
   private bool hasReachedTarget;

   // cached the transform component
   void Awake()
   {
     _transform = transform;
   }

   IEnumerator Start()
   {
     foreach(Transform t in locations)
     {
         StartCoroutine(MoveTo(_transform, t.position, timeToTake));
         while (!hasReachedTarget)
             yield return null;
     }
   }

   IEnumerator MoveTo(Transform objectToMove, Vector3 targetPosition, float timeToTake)
   {
     float t = 0;
     Vector3 originalPosition = objectToMove.position;
     hasReachedTarget = false;
     while (t < 1) {
         t += Time.deltaTime / timeToTake;
         objectToMove.position = Vector3.Lerp(originalPosition, targetPosition, t);
         Instantiate(trail, _transform.position, Quaternion.identity);
         yield return null;
     }
     hasReachedTarget = true;
   }
 }

Result:

alt text

Credits to the original MoveTo function goes to @whydoidoit from UnityGems (Specifically the Coroutines++ tutorial)


grav.png (23.2 kB)
Comment
Add comment · 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

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

16 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

Related Questions

How to add collider2D to LineRenderer? 0 Answers

Generating lines between procedurally instantiated gameobjects, within a certain radius 1 Answer

Drawing 2d line from a point on an object! 1 Answer

How to draw a line in unity3d 3 Answers

Gameobject is drawing a line render,when it's not supposed to ! 0 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