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 h00man · May 30, 2019 at 12:31 PM · physicsprojectiletrajectorycurved path

help with shooting projectile along a curve path

hey guys, so i been trying to find a way to shoot projectiles along a curve path . and i found this tutorial on youtube which was really good. here (https://www.youtube.com/watch?v=IvT8hjy6q4o)

i also uploaded the project in case you wanted to check it out. here (http://www.mediafire.com/file/z5zfqbcnpznnbsh/arc+luncher.unitypackage)

but there is problem with this script .if i move the target object somewhere above the starting point the curved path will be disappeared and i get couple of errors.in fact when ever the path is about to become a straight line the errors will pop up . here is the screenshot of errors (https://ibb.co/vZ5JTZ2) and here is a gif file to show when the problem shows up (https://i.stack.imgur.com/ibCD9.gif) and finally here is the code responsible for calculating the curve path a lunching projectiles.

 public Transform startPoint;
     public Transform target;
     public int resolution = 30;
     public float curveHight = 25;
     public float gravity = -18;
 
     
     public Rigidbody bullet;
     public LineRenderer lineRenderer;
     void Start()
     {
 
     }
 
     void Update() {
 
 
 
 
         DrawPath();
 
         
         if (Input.GetKeyDown(KeyCode.Space))
         {
             Launch();
         }
 
        
     }
 
     void Launch()
     {
 
         Rigidbody clone = Instantiate(bullet, startPoint.position, Quaternion.identity);
 
         Physics.gravity = Vector3.up * gravity;
 
         clone.velocity = CalculateLaunchData().initialVelocity;
     }
 
     LaunchData CalculateLaunchData()
     {
         float displacementY = target.position.y - startPoint.position.y;
         Vector3 displacementXZ = new Vector3(target.position.x - startPoint.position.x, 0, target.position.z - startPoint.position.z);
         float time = Mathf.Sqrt(-2 * curveHight / gravity) + Mathf.Sqrt(2 * (displacementY - curveHight) / gravity);
         Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * curveHight);
         Vector3 velocityXZ = displacementXZ / time;
 
         return new LaunchData(velocityXZ + velocityY * -Mathf.Sign(gravity), time);
     }
 
     void DrawPath()
     {
         LaunchData launchData = CalculateLaunchData();
         Vector3 previousDrawPoint = startPoint.position;
 
 
         for (int i = 1; i <= resolution; i++)
         {
             float simulationTime = i / (float)resolution * launchData.timeToTarget;
             Vector3 displacement = launchData.initialVelocity * simulationTime + Vector3.up * gravity * simulationTime * simulationTime / 2f;
             Vector3 drawPoint = startPoint.position + displacement;
             Debug.DrawLine(previousDrawPoint, drawPoint, Color.green);
 
             previousDrawPoint = drawPoint;
 
             lineRenderer.positionCount = resolution;
 
             lineRenderer.SetPosition(i - 1, drawPoint);
         }
     }
 
     struct LaunchData
     {
         public readonly Vector3 initialVelocity;
         public readonly float timeToTarget;
 
         public LaunchData(Vector3 initialVelocity, float timeToTarget)
         {
             this.initialVelocity = initialVelocity;
             this.timeToTarget = timeToTarget;
         }
 
     }
 }

so,any idea what might be causing the the problem?

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
1

Answer by xxmariofer · Jun 03, 2019 at 01:55 PM

that error is because displacementY is higher than the gravity, so that ends up with a negative value and you cant do mathf.sqrt of negative values, thats giving you an inifint vlue and you cant move objects to infinite positions so is giving the assetion code, try changing your calculatelaunchdata function to this, but this is just clamping so is not negative in case you want the curve to be negative you will need some extra work code:

 LaunchData CalculateLaunchData()
 {
     float displacementY = target.position.y - startPoint.position.y;
     Vector3 displacementXZ = new Vector3(target.position.x - startPoint.position.x, 0, target.position.z - startPoint.position.z);

     float value = (displacementY - curveHight) / gravity;
     float clampedCurve = Mathf.Clamp(value, 0, value);

     float time = Mathf.Sqrt(-2 * curveHight / gravity) + Mathf.Sqrt(2 * clampedCurve);
     Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * curveHight);
     Vector3 velocityXZ = displacementXZ / time;

     return new LaunchData(velocityXZ + velocityY * -Mathf.Sign(gravity), time);
 }
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 h00man · Jun 03, 2019 at 03:04 PM 0
Share

THAN$$anonymous$$S a lot man i been struggling with this for days now and you just solve it (:

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

178 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

Related Questions

2D projectile trajectory prediction 2 Answers

How to find cannon elevation angle to fire a projectile to known range? 1 Answer

How to: Lead on a projectile trajectory. Can't get T without angle without lead without T without angle etc... 0 Answers

How do I arc a rigidbody so it lands on a target? 2 Answers

Aim preview for projectile trajectory 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