• 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 SBull · Sep 11, 2015 at 04:37 PM · javascriptvector3arrayslinerenderertrajectory

Having trouble using a line renderer to show a projectile's predicted trajectory.

I have a script that I have been using to show a projectile's predicted trajectory in a 2D game. It is basically a click, drag and release slingshot. This script currently creates an array of primitive sphere gameobjects to show where the projectile is going to go. It works great and lines up with the projectile's path perfectly, but it doesn't look all that great and I really don't want to have it creating all of those spheres every time the player takes aim. I have been trying to use a line renderer to take the place of the spheres, but after looking up every line rendering and trajectory tutorial I could find, I still can't figure out what I'm doing wrong.

I have looked over the line renderer scripting API many times and it's clear I'm supposed to be using the SetPosition function, but with my own formula. I just can't figure out how to get it to work for me. I think the problem has to do with the number of vector3 positions in the line renderer getting set wrong at some point. If I check the positions before touching anything, it shows the amount set in the Inspector, however as soon as I click on the projectile, it gets set to 0, which I'm guessing is why the line renderer is not shown. I'm just not sure what is causing it to get reset.

Here is my current code (with the sphere method). If someone could tell me the correct way to apply a line renderer to this I would be extremely grateful.

 var force = 11;  // Distance the dots cover. Must change projectile's 
                   //launchForce to equal amount to follow path correctly.
 var samples = 40; // Number of dots. Increase to extend how far the line reaches.
 var spacing = 0.1; // Time between samples
      
 private var offset : Vector3;
 private var home : Vector3;
 private var argo : GameObject[];
 private var velocity = Vector3.zero;
 
 var projectileLoaded : boolean;
 
 function Start () 
 {
        argo = new GameObject[samples];
     
        for (var i = 0; i < argo.Length; i++) 
        {
            var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            go.collider.enabled = false;
            go.transform.localScale = Vector3(0.4, 0.4, 0.4);
            argo[i] = go;
        }
 }
 
 function ShowHideIndicators(show : boolean)
 {
     for(var i = 0; i < argo.Length; i++)
     {
         argo[i].renderer.enabled = show;
         argo[i].transform.position = home;
     }
 }
 
 function OnMouseDown()
 {
     if(projectileLoaded)
     {
         home = transform.position;
         ShowHideIndicators(true);
     }
 }
      
 function OnMouseDrag()
 {
     if(projectileLoaded)
     {
         DisplayIndicators();
     }
 }
      
 function DisplayIndicators()
 {    
     argo[0].transform.position = transform.position;
     var v3 = transform.position;
     var y = (force * (home - transform.position)).y;
     var t = 0.0;
     v3.y = 0.0;
     
     for (var i = 1; i < argo.Length; i++)
     {
         v3 += force * (home - transform.position) * spacing;
         t += spacing;
         v3.y = y * t + 0.5 * Physics.gravity.y * t * t + transform.position.y;
         argo[i].transform.position = v3;
     }
 }
 
 //////////////////////////////////////////////////////////////////
 /////// I know I need to use the SetPosition function shown here, 
 /////// but don't know how to fit it with my current formula.

 var pathNodes : Vector3[];
 function Start()
 {
     var lineRenderer : LineRenderer = gameObject.AddComponent.<LineRenderer>();
 }
     
 function Update()
 {
     var lineRenderer : LineRenderer = GetComponent.<LineRenderer>();
     lineRenderer.SetVertexCount(pathNodes.length);
     for (var i: int = 0; i < pathNodes.length; i++)
     {
         lineRenderer.SetPosition(i, pathNodes[i].transform.position);
     }
 }
Comment
Add comment · Show 4
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 tarabout · Jul 19, 2016 at 03:28 AM 0
Share

@SBull I am looking into doing the same thing, were you able to find a way to do this?

avatar image SBull · Jul 19, 2016 at 04:12 AM 0
Share

Unfortunately, no. I settled for swapping the spheres out with alpha textured planes.

avatar image tarabout SBull · Jul 19, 2016 at 04:14 AM 0
Share

Ah, well if that works! I'll keep searching!

Show more comments

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.

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

32 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

Related Questions

How to create a 2D parabolic trajectory prediction line with javascript? 1 Answer

Saving Certain Vector3's positions 2 Answers

Is this considered bad coding 1 Answer

Instantiate multiple objects from an array 1 Answer

Failed setting triangles in my mesh 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