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 /
  • Help Room /
avatar image
0
Question by mmaalex22112 · Feb 16, 2018 at 07:24 AM · vector3shooting2d-physicsprojectilearc

Shooting A Projectile With an Arc

Hey all, I've been working on a script to launch a projectile in an arc and I've managed to cobble this together. EDIT: With the current code, the projectile is shot, but the calculation seems to be off. It will always overshoot the player. :/ EDIT 2: So I just calibrated the numbers and it seems to be fairly accurate now.

Help would be appreciated. Thanks In advance!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TripleArcShoot : MonoBehaviour {
 
 
     public Vector3 targetPos;
     public float speed = 10;
     public float arcHeight = 3;
     GameObject enemy;
     public float DistanceFromPlayer, CoolDown;
     public int AttackRadius;
     public Vector3 InstPos;
     public GameObject instantiatedObj;
     public GameObject ProjectileGM;
 
     private Vector3 ShootDir;
     public float ShotSpeed;
     private Vector3 EnemyPos;
     private bool FL, FR;
 
 
 
 
     void Start()
     {
         enemy = GameObject.FindGameObjectWithTag("Player");
         targetPos = enemy.transform.position;
         AttackRadius = 15;
         CoolDown = 5;
     }
 
     void Update()
     {
         if (enemy != null)
         {
             DistanceFromPlayer = Vector3.Distance(enemy.transform.position, this.gameObject.transform.position);
             if (DistanceFromPlayer <= AttackRadius)
             {
                 Attack();
             }
         }
     }
 
     void Attack()
     {
         if (transform.position.x > enemy.transform.position.x)
         {
             InstPos = transform.position + new Vector3(-1.5f, 0f, 0f);
             FL = true;
         }
         else
         {
             InstPos = transform.position + new Vector3(1.5f, 0f, 0f);
             FR = true;
         }
 
         CoolDown -= Time.deltaTime;
         if (CoolDown <= 0)
         {
             Debug.DrawLine(transform.position, enemy.transform.position, Color.cyan);
             EnemyPos = enemy.transform.position - transform.position;
             ShotSpeed = Mathf.Sqrt(Mathf.Abs(EnemyPos.x) / 0.0781678003f);
 
             if (FR)
                 ShootDir = new Vector3(1.0f, 1.19175f, 0f);
             else
                 ShootDir = new Vector3(-1.0f, 1.19175f, 0f);
 
 
             instantiatedObj = (GameObject)Instantiate(ProjectileGM, InstPos, transform.rotation);
             instantiatedObj.GetComponent<Rigidbody2D>().velocity = ShootDir.normalized * ShotSpeed * .80f;
             Destroy(instantiatedObj, 4);
             CoolDown = 5f;
         }
     }
 
     static Quaternion LookAt2D(Vector2 forward)
     {
         return Quaternion.Euler(0, 0, Mathf.Atan2(forward.y, forward.x) * Mathf.Rad2Deg);
     }
 }
Comment
Add comment · Show 6
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 meat5000 ♦ · Feb 11, 2018 at 09:37 AM 0
Share

Go Back To Basics!!

https://www.bbc.co.uk/education/guides/zrsdmp3/revision/1

avatar image mmaalex22112 meat5000 ♦ · Feb 13, 2018 at 06:15 AM 0
Share

Hey @meat5000

I went back to basics, this is what I've got now. The new issue is that the calculation causes it to consistently overshoot the player, in either direction. Thanks for your re$$anonymous$$der to strip it back to black :)

avatar image meat5000 ♦ mmaalex22112 · Feb 15, 2018 at 09:41 AM 0
Share

Yeah sorry didnt mean to seem condescending with the 'BiteSize' :P Sometimes that kind of stuff re$$anonymous$$ds us things we missed.

You seem to have a lot of hardcoded numbers in there, not to mention the $$anonymous$$athf.Abs. You know, at a glance these things are likely adding error.

Luckily there seem to be a lot of examples kicking around the internet. $$anonymous$$aybe not the best chosen battle to re-invent the wheel. Just be all, 'Ah thats how its done' and save your brainpower for the next task ;)

https://www.google.co.uk/search?q=unity+calculating+an+arc&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&dcr=0&ei=JFWFWtryIfGBtgeCwJzAAg

https://vilbeyli.github.io/Simple-Trajectory-$$anonymous$$otion-Example-Unity3D/

Show more comments

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

96 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

Related Questions

Projectile velocity correction to always hit stationary spot 0 Answers

Calculating a projectile arc 0 Answers

How to make paint characters shoot each other? 1 Answer

Can't make my AI shoot projectiles with raycast 0 Answers

Rotate Vector to hit.normal only by set degrees 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