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 SaiSrinivas05 · May 07, 2020 at 05:22 PM · aimingtrajectoryarc

trajectory arc not working, how to build a trajectory arc for aiming

I want to build a trajectory arc when I am dragging my mouse ie I want an aiming system and my code isn't working.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class movement: MonoBehaviour
 {
     public float velocity;
     float time;
     float x;
     float y;
     float tt;
     float g;
     Vector2 force;
     public float power = 2.0f;
     Vector3 startpoint;
     Vector3 endpoint;
     Camera cam;
     public Vector2 maxpower;
     public Vector2 minpower;
     public Rigidbody2D rb;
     Vector3 currentposition;
     Vector3 sp;
     LineRenderer lr;
     int resolution = 10;
     Vector3 newpoint;
     // Start is called before the first frame update
     void Start()
     {
         time = 0f;
         g = Mathf.Abs(Physics2D.gravity.y);
         cam = Camera.main;
         lr = GetComponent<LineRenderer>();
     }
 
     // Update is called once per frame
     void Update()
     {
         time += Time.deltaTime;
         x = gameObject.transform.position.x + velocity * time;
         tt = time * time;
         y = gameObject.transform.position.y + (g * tt) / 2f;
         if(Input.GetMouseButtonDown(0))
         {
             startpoint = cam.ScreenToWorldPoint(Input.mousePosition);
             startpoint.z = 5;
         }
         if(Input.GetMouseButton(0))
         {
             sp = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 5);
             currentposition = cam.ScreenToWorldPoint(Input.mousePosition);
             currentposition.z = 5;
             LineRenderer(sp);
         }
         if (Input.GetMouseButtonUp(0))
         {
             endpoint = cam.ScreenToWorldPoint(Input.mousePosition);
             endpoint.z = 5;
             force = new Vector2(Mathf.Clamp(startpoint.x - endpoint.x, minpower.x, maxpower.x), Mathf.Clamp(startpoint.y - endpoint.y, minpower.y, maxpower.y));
             rb.AddForce(force * power, ForceMode2D.Impulse);
             x = x + velocity * time;
             y = y + (g * tt) / 2f;
             EndLine();
         }
     }
     public void LineRenderer(Vector3 p)
     {
         lr.positionCount = resolution;
         Vector3 arc = p;
         for(int i=0;i<resolution;i++)
         {
             newpoint = calculate(arc, i / (float)resolution);
             lr.SetPosition(i, newpoint);
             arc = newpoint;
         }
     }
     public Vector3 calculate(Vector3 point, float t)
     {
         point.x += velocity * t;
         point.y += 0.5f * g * t * t;
         return point;
     }
     public void EndLine()
     {
         lr.positionCount = 0;
     }
 }
 
 



This is the code, the drag and move are working but the arc is not executing. Please help.

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

124 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

Related Questions

How to arc rigidbody2D velocity to hit target gameObject 1 Answer

Moving the player in a ballistic trajectory in 3D 0 Answers

AI Turret Aiming 2 Answers

Rendering a 2D trajectory arc 1 Answer

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