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 mickey-evers · Jan 15, 2018 at 02:32 PM · 2d game2d-physicslinerenderervectorprediction

Unity prediction line renderer

I am making a 2D game in Unity where I have to throw something. To let the player know where he is throwing at I am trying to show a prediction line renderer on the screen so the player can see where the ball is going to end. The code is partly working. It shows the line, but it is not really. Is it possible to make it 100% accurate?

You can find my code below. The on mouseUp ads the force to my object that I throw while the dragging function "measures" how mutch forse should be added. The DisplayTrajectoryLineRenderer2 renders the line but not accurate enough. Can somebody help me with this problem?

 void OnMouseUp () {
     ball.isKinematic = false;
     ball.AddForce (currentForce * forceMultiplier);
     ball.AddTorque (Random.Range(-torqueMultiplier,torqueMultiplier));
     timer = timerSlider.value;
     clickedOn = false;
     thrown = true;
 }
 
 void Dragging () {
     mouseWorldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     catapultToMouse = mouseWorldPoint - catapult.position;
 
     if (catapultToMouse.sqrMagnitude > maxStretchSqr) {
         rayToMouse.direction = catapultToMouse;
         mouseWorldPoint = rayToMouse.GetPoint (maxStretch);
     }
 
     mouseWorldPoint.z = 0f;
     transform.position = mouseWorldPoint;
     Debug.DrawLine (catapult.position, mouseWorldPoint, Color.red);
 
     currentForce = catapult.position - mouseWorldPoint;
 
 }    
 void DisplayTrajectoryLineRenderer2()
 {
 
     SetTrajectoryLineRenderesActive(true);
 
     int segmentCount = 20;
     float segmentScale = 2;
 
     Vector3 maxPosition = (catapult.position-mouseWorldPoint).normalized * 1.5f + catapult.transform.position;
     Vector3 testForce = currentForce * forceMultiplier;
 
     float test = Vector3.Distance (catapult.transform.position, mouseWorldPoint);
 
     Vector3 v2 =  maxPosition - mouseWorldPoint;
 
     Vector2[] segments = new Vector2[segmentCount];
 
     segments[1] = mouseWorldPoint;
 
     Vector2 segVelocity = new Vector2(v2.x, v2.y) * torqueMultiplier * test;
     //Vector2 testVel = currentForce * forceMultiplier;
 
     for (int i = 1; i < segmentCount; i++) {
         // Time it takes to traverse one segment of length segScale (careful if velocity is zero)
         float time2 = i * Time.fixedDeltaTime * 5;
 
         // Add velocity from gravity for this segment's timestep
         segments[i] = segments[0] + segVelocity * time2 + 0.5f * Physics2D.gravity * Mathf.Pow(time2, 2);
     }
     TrajectoryLineRenderer.SetVertexCount(segmentCount);
     for (int i = 0; i < segmentCount; i++)
         TrajectoryLineRenderer.SetPosition(i, segments[i]);
 }

  
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

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.

Follow this Question

Answers Answers and Comments

83 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

Related Questions

Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player 0 Answers

How to refract a linerenderer through a 2d object using Physics2D ? 0 Answers

How can I make sure triggers or collisions are never skipped even for high speeds? 2 Answers

Simple 2D Character Movement does not work properly 1 Answer

BoxCollider2D have an offset on collision,Collider2D has an collision offset? 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