Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by isaacduncan02 · Jun 21, 2018 at 08:14 AM · physicslinerenderer

How to make an Object follow a projectile motion pathway

Beginner Here. I was watching a tutorial on how to make a pathway projection using line renderer. And it worked. I can input a initial velocity and a angle and it renders the path it would follow. But now I want to have an object follow this pathway. How would I go on about this?

Pathway Projection Code

 void RenderArc()
     {
         lr.positionCount = resolution + 1;
         lr.SetPositions(CalculateArcArray());
     }
 
     //calculates the points needed to the arc
     Vector3[] CalculateArcArray()
     {
         Vector3[] arcArray = new Vector3[resolution + 1];
     
         radianAngle = Mathf.Deg2Rad * angle;
         float maxDistance = (velocity * velocity * Mathf.Sin(2 * radianAngle)) / gravity;    
 
         for(int i = 0; i <= resolution; i++)
         {
             float t = (float)i / (float)resolution; //ensures all points are equally spaced
             arcArray[i] = CalculateArcPoint(t, maxDistance);
         }
 
         return arcArray;
 
     }
 
     //calculate height and distance of each vertex
     Vector3 CalculateArcPoint(float t, float maxDistance)
     {
         float x = t * maxDistance;
         float y = x * Mathf.Tan(radianAngle) - ((gravity * x * x) / (2 * velocity * velocity * Mathf.Cos(radianAngle) * Mathf.Cos(radianAngle)));
         return new Vector3(x, y);
     }


Comment
Add comment · Show 5
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 RetroGeek46 · Jun 26, 2018 at 08:56 AM 0
Share

I recently made a system where you have bullets that can ricochet off the walls, allowing sniping of enemies behind walls etc. A laser (line renderer) shows the trajectory a bullet would follow (which it does upon shooting). But it uses Raycasts and Reflect to calculate the trajectory. If you want to see how that worked I could tell, but it is quite different than the approach you have here.

avatar image isaacduncan02 RetroGeek46 · Jun 26, 2018 at 09:16 AM 0
Share

$$anonymous$$ay I see the code? Am still quite new at Unity but I learn quite well by just reading and understanding other people's code.

avatar image RetroGeek46 isaacduncan02 · Jun 26, 2018 at 09:20 AM 0
Share

It is a recursive function that runs depending on how many relfections you want

 private void CalculateTrajectory(Vector3 position, Vector3 direction, int reflectionsRemaining) {
     if (reflectionsRemaining <= 0) {
         return;
     }
     Vector3 start = position;
     Ray ray = new Ray(position, direction);
     if (Physics.Raycast(ray, out RaycastHit hit, maxDistance)) {
         direction = Vector3.Reflect(direction, hit.normal);
         position = hit.point;
     }
 
     } else {
         position += direction * maxDistance;
     }
     DrawLine(start, position);
     CalculateTrajectory(position, direction, reflectionsRemaining - 1);
 }
 //Function to draw line based on calculated trajectories
 private void DrawLine(Vector3 start, Vector3 end) {
     GameObject myLine = new GameObject {
         tag = currentTag
     };
     myLine.transform.position = start;
     myLine.AddComponent<LineRenderer>();
     LineRenderer lr = myLine.GetComponent<LineRenderer>();
     lr.startWidth = 0.05f;
     lr.endWidth = 0.05f;
     lr.SetPosition(0, start);
     lr.SetPosition(1, end);
 }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Jun 21, 2018 at 08:33 AM

Good day.

IF you know all the points (vertex) of the line redered, you only need to make the object go to the 1 by 1.

You can use the function Lerp to smooth the movement.

something like this:

object.transform.position = Vector3.Lerp(object.transform.position, NextVertex.transform.position, 0.2f)

(Take not i'm using t parameter as 0.2 to do a very very samooth movement, but of course you can try to increase it up to 1)

Bye!

Comment
Add comment · Show 1 · 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 isaacduncan02 · Jun 26, 2018 at 07:56 AM 0
Share

Is there a way of using AddForce or Velocity? Because I want the object to be interrupted by other walls or other objects. So for example the ball initially follows the ball but a wall can block it. (I don't need the line renderer to show the path of the ball after bouncing). Thanks!

avatar image
0

Answer by rrcsapp · Jan 29, 2019 at 05:28 AM

I found an amazing tutorial regarding the trajectory projectile motion. this might help you a lot.

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

150 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

Related Questions

How can i create a character like dragon hill game, Digging underground, what collider should i use? 0 Answers

A method to update a Vertex draw every step. 1 Answer

2D trajectory prediction around planets 1 Answer

Bouncing Raycast "Laser" Not Working? 1 Answer

change boxcollider2D length during runtime 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