• 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 Chocolade · Feb 21, 2017 at 10:59 PM · c#scripting problemscript.drawline

How can i draw a blue line in scene window show what way the character already walked ?

 int index = 0;
     private void WayPoint()
     {
         if (index == waypoints.Length)
             index = 0;
         target = waypoints[index].transform;
         float distance = Vector3.Distance(myTransform.position, target.transform.position);
         Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
         Debug.DrawLine(target.transform.position, myTransform.position, Color.blue);
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
         //move towards the player
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         if (distance < 2f)
             index++;
     }

Now i'm drawing a red line that show the next target(waypoint) the character will go to. But i want to add a blue line that will show what way the character already did from the original start position and all visited waypoints until the real time character position. So when the character will walk between the waypoints it will show two lines red for the next target and blue what was visited already.

This WayPoint() method i call it from the Update() function.

I just added the same line like the Red color with Blue color i just can't figure out how to draw the line between the current character position and where he visited so far.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Feb 21, 2017 at 11:16 PM

 Debug.DrawLine(waypoints[index-1].transform.position, myTransform.position, Color.blue);
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 UnityCoach · Feb 21, 2017 at 11:17 PM 0
Share

if you want to draw several lines over time, you're going to have to put them in a list and use a for loop to draw them all.

avatar image Chocolade · Feb 22, 2017 at 02:11 AM 0
Share

Are you sure it's waypoints[index-1] ? I'm getting exception on it since index is 0 and -1 make index = -1 if i change it to waypoints[index] i'm not getting the exception.

avatar image UnityCoach Chocolade · Feb 22, 2017 at 06:27 PM 0
Share

Ah yes, you've got to catch that exception with :

 if (index > 0)
     Debug.DrawLine(waypoints[index-1].transform.position, myTransform.position, Color.blue);
avatar image
0

Answer by Zee-Play · Feb 22, 2017 at 03:50 AM

Do you mean something like this?

Excuse the crude Mspaint ;)

alt text


untitled.png (4.1 kB)
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 Chocolade · Feb 22, 2017 at 06:04 AM 0
Share

Zee-Play something like this i'm adding an image and will explain here:

Example

The egg the big circle is the Player that walk.

The left side:

The first blue line on the bottom on the left it's left side is the original egg(big circle) original start position. Then the first small black square it was the first waypoint the egg(big curcle) visited already.

Then it was walking up right to the next small black square this is the second visited waypoint the second black square.

And now the longer blue line is connected to the egg(the player the big circle) this is what the player was walking already after the last visited waypoint.

Now the right side of the egg:

The red line show the next waypoint to be visit. The player will walk until the nexy way point to visit thats the right black box on the red line side.

So this is my logic steps:

  1. When the player is move there will be in real time two lines connected to the player. Blue and Red. The Blue will show visited already waypoints including the way the player walked to them. I want to draw the walking directions with the blue line. And the blue line will keep be drawing in real time while the player is keep moving.

  2. The red line is showing only the next way point to visit. Also in real time.

In the image in the logic if the player now move to the next waypoint on the right the red line will be get shorter and shorter as the player is walking to this waypoint and the blue line will be longer and longer.

In the end i will get something like a map of all visited waypoints with a blue line. Including turns and change directions the player did like i drawed it with the blue line.

avatar image Chocolade · Feb 22, 2017 at 06:09 AM 0
Share

In the end it should look something like this. The big circle egg on the right is the player after visited all waypoints. And on the left is the route of all visited waypoints and the way he moved what direction he walked to each waypoint including the original start position.

Yes the word i missed was Route that's the blue line the route the playes walked.

Anyway the line

 Debug.DrawLine(waypoints[index].transform.position, myTransform.position, Color.blue);

Didn't draw a blue line at all even if i removed the red line code line for testing it didn't draw the blue line not sure why.

Route

ss59.jpg (53.3 kB)
avatar image Zee-Play Chocolade · Feb 22, 2017 at 11:45 AM 0
Share

Here's how I'd do it. You'll want the Debug.DrawLine in your Update method so it's drawn every frame.

 void Update()
 {
     if (waypoints.Length >= 1)
     {
         // draw a line between the player and the last checkpoint //
         Debug.DrawLine(waypoints[waypoints.Length - 1].transform.position, myTransform.position, Color.blue);
     }
     if (waypoints.Length >= 2)
     {
         // create lines between each checkpoint //
         for (int i = 0; i < waypoints.Length-1; i++)
         {
             Debug.DrawLine(waypoints[i].transform.position, waypoints[i+1].transform.position, Color.blue);
         }
     }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

298 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 can i make float u = 1 – t; ? I'm getting errors 1 Answer

How can i create two buttons in the inspector in editor with space between the buttons without overriding existing other controls ? 1 Answer

How can i make both two cameras to follow the player but only one with control on player ? 0 Answers

Why when i loop over the vertices it's not showing the mesh ? 1 Answer

Why none of the events OnTriggerEnter or OnCollisionEnter not fire ? 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