• 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 Mishki · Aug 19, 2013 at 05:21 AM · c#trajectory

How to project the trajectory of a spaceship?

So I am making a flight simulator, and I would like to use a line renderer to project the ships current trajectory based on the currently inputed pitch, yaw and forward vector speed. For some reason I seem to just be drawing a complete blank as to how to do this mathematically. If anyone could help me out with this basic trajectory plotting it would be greatly appreciated.

Here is some of my code for reference to see what i'm working with.

         yawMod = ((Input.mousePosition.x-(Screen.width/2))/(Screen.width/2));
         pitchMod = ((Input.mousePosition.y-(Screen.height/2))/(Screen.height/2));
         
         if(flightControl)
         {
             rotationStep = new Vector3(pitchSpeed * pitchMod, yawSpeed * yawMod, 0f);
             transform.Rotate(rotationStep* Time.deltaTime);
         }
         speedStep = Vector3.forward * speedCur;
         transform.Translate(speedStep * Time.deltaTime);
         UpdateTrajectory ();

I am storing the points for the line renderer in a Vector3 array called trajectoryPoints and updating the line renderer positions once I have calculated all the points.

I was able to get it to work using an empty game object and moving it and rotating it, but I would like to do this mathematically so that I can calculate intercepts and be able to build an AI system off of it.

EDIT:

Ok so I finally managed to get it to replicate what I want the line to look like. However I dont think I am conciously understanding the math being involved and not sure how I would go about using what I have to say grab the exact position the ship should be at at a certain point on the line.

 void UpdateTrajectory ()
     {
         int i = 1;
         Vector3 trajectoryStep;
         Vector3 trajectoryCur = Vector3.forward;
         trajectoryStep = transform.position;
         trajectoryPoints[0] = trajectoryStep;
         trajectoryLine.SetPosition(0,trajectoryPoints[0]);
         while(i < trajectoryPoints.Length)
         {
             trajectoryStep = trajectoryPoints[i-1] + (speedStep.magnitude * trajectoryCur);
             trajectoryCur = Quaternion.Euler(rotationStep) * trajectoryCur;
             trajectoryPoints[i] = trajectoryStep;
             trajectoryLine.SetPosition(i,trajectoryPoints[i]);
             i++;    
         }
     }
Comment
Add comment · Show 2
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 DaveA · Aug 19, 2013 at 05:24 AM 0
Share

Is there gravity or drag involved? Or do you just want to draw a straight line from the ship out along it's forward direction?

avatar image Mishki · Aug 19, 2013 at 05:30 AM 0
Share

I currently do not plan on adding them in, but I am just working up a prototype right now to test how resource intensive all my systems will be.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robhuhn · Aug 19, 2013 at 07:41 AM

I made a quick and dirty test - May be it will help you. If you add this script to an object, you can change the angle with left/right - up/down and change the velocity using Axis Forward (need to be set up in unity).

Instead of a line renderer I used Vectrosity. That saved me some time to render the line for debugging.

 using UnityEngine;
 using System.Collections;
 using Vectrosity;
 
 public class Trajectory : MonoBehaviour 
 {
     private VectorLine line;
     
     void Start () 
     {
         line = new VectorLine("trajectory", new Vector3[2], null, 1f);
     }
     
     
     void Update () 
     {
             //get the rotation
         Vector3 eulerRotation = Vector3.zero;
         eulerRotation.y = Input.GetAxis("Horizontal") * 20f;
         eulerRotation.z = Input.GetAxis("Vertical") * 20f;
         transform.rotation = Quaternion.Euler(eulerRotation);
         
         //prepare the line points

             int numSteps = 50;
         Vector3[] points = new Vector3[numSteps];
         line.Resize(points);

             //simulate some steps
         for (int i = 0; i < numSteps; i++) 
         {
             float x = i / 10f;
             float vel = 2f + Input.GetAxis("Forward") * 10f; //should be the objects velocity
             float direction = -eulerRotation.z / 10f;
             float y =  -direction * Mathf.Pow(x / vel, 2f) / 2f;
             
             direction = eulerRotation.y / 10f;
             float z = -direction * Mathf.Pow(x / vel, 2f) / 2f;
             
             points[i] = new Vector3(x, y, z); //the resulting point
         }
         
         line.Draw3D(transform);
     }
 }


[1]: http://en.wikipedia.org/wiki/Trajectory

Comment
Add comment · Show 2 · 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 Mishki · Aug 19, 2013 at 04:42 PM 0
Share

Well I do not have Vectosity, so I cannot use the code as is. I am trying to break it appart to work with the line renderer, but I am getting confused as to where all the numbers you are using are co$$anonymous$$g from. Are the *20f to the rotations just an arbitrary multiplier to give it a decent size? the same goes for the velocity, is there a reason for the 2f and the *10f in it? why are you making x / 10f?

avatar image robhuhn · Aug 20, 2013 at 06:46 AM 0
Share

all the numbers are just for test purposes:

  • 20f to the rotations is just a multiplier to get a rotation from -20 to 20. In your case it should be yaw$$anonymous$$od and pitch$$anonymous$$od or rotationStep.x and rotationStep.y.

    • 2f + Input.GetAxis("Forward") 10f* should be your velocity. Rigidbody.velocity or (currentPos - previousPos) / Time.deltaTime (m/s) or something like that in your case.

  • = i / 10f* is just an interpolation to get the points closer to each other. $$anonymous$$g. if you have 100 steps (numSteps = 100) and x = i then the line would have a length of 100 meters (assumed y and z is 0 otherwise it would be a bit longer due to it's bending factor). You would replace that by values which will fit your game. In my case the line has 50 points within 5 meters (50 / 10 or numSteps / 10f).

You don't need Vectosity, that was also just for testing and debugging to render a line quickly. You would take the resulting points and pass them to the line renderer.

Another important note is that line.Draw3D(transform); will draw the line locally. So if you use line renderer you would need to add the spaceships position and rotation.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Show trajectory path a ball will take based on collision 1 Answer

2D bullet spray problems 1 Answer

Trajectory Simulation for Carrom game when stricker collided to slam( Coin/Token ) 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