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 mrpmorris · Dec 15, 2016 at 09:11 PM · physicsballistic

How can I solve ballistic angle and velocity to hit a specific point after a specific amount of time?

I have two objects, missile and target. I want to launch missile at target and have it arrive in exactly X seconds.

I know the locations of both objects, I know the mass of missile, and I know the value of gravity. Does anyone have a C# method that calculates the angle and force required to hit the target after the exact given duration?

I am using the physics engine in Unity3D, so I don't need to write code to move the projectile. I just need to know how to work out the initial Vector.

Comment
Add comment · Show 1
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 Glurth · Dec 15, 2016 at 11:29 PM 0
Share

Super useful page for this: check out the "time of flight" section, and angle required sections in particular https://en.wikipedia.org/wiki/Trajectory_of_a_projectile

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by mrpmorris · Dec 16, 2016 at 02:13 PM

Bah. I can't believe how simple the answer was. My problem was that I assumed using Rigidbody.AddForce on a stationary object would be the equivalent of setting Rigidbody.velocity - it's not.

So, using basic school physics, here is the answer.

 void Update()
 {
     if (Input.GetMouseButton(0))
     {
         Vector3 vector = CalculateTrajectoryVelocity(transform.position, Target.transform.position, 5);
         Rigidbody.velocity = vector;
     }
 }

 Vector3 CalculateTrajectoryVelocity(Vector3 origin, Vector3 target, float t)
 {
     float vx = (target.x - origin.x) / t;
     float vz = (target.z - origin.z) / t;
     float vy = ((target.y - origin.y) - 0.5f * Physics.gravity.y * t * t) / t;
     return new Vector3(vx, vy, vz);
 }
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
avatar image
0

Answer by SpankPack · Dec 15, 2016 at 11:40 PM

You have to experiment with speeds of the projectile OR calculate the speed needed to get the time required. Standard highschool physics. Hint: horizontal velocity is: velocity * cos(a) where a is your cannon angle. Velocity is distance divided by time. You know distance you know time. Calc the angle a.

Then check the below autoaimer. This script is shooting hamburgers on passing players from a barrel of a cannon that turns towards the player real time. It uses the cannonmans rule, but does not correct too much for targets height.

using UnityEngine; using System.Collections;

public class AutoAimer : MonoBehaviour {

 public Transform target = null;             // The poor object to be aimed at
 public float range = 100f;                    // The range the aimer should start aiming. 100f -ish
 public float aimRotationSpeed = 50.0f;        // 50f -ish
 public Fomp _Fomp;                            // The script taking care of shooting (fomping Hamburgers at you)

 Quaternion targetRotation;
 float distance;
 Vector3 newDirection;
 float theAngle;
 float myProjectileSpeed = 30f;
 Collider[] targetColliders;                // The passing players
 bool isTargetFound = false;


 void Start(){
     //_Fomp may have to be instantiated.
     _Fomp = GameObject.FindObjectOfType (typeof(Fomp)) as Fomp;
 
 }

 void Update () 
 {
     if (!isTargetFound && _Fomp != null) {
         //Tell the Fomper to stop wasting Hamburgers
         _Fomp.Fomper(false);
         GetTarget ();
     }
     else
     {
         //Tell the fomper to start fomping Hamburgers
         if(_Fomp != null){
             _Fomp.Fomper(true);
         }
         //Check if the target is still on range.
         if (distance > range && _Fomp !=null)
         {
             isTargetFound = false;
             //Start acquiring other targets
             _Fomp.Fomper(false); // Stop fomping too
             //Debug.Log ("----->Target out of range.");
         }
     }

     if (_Fomp == null)
     {
         Debug.LogError("ZOMG! Autoaimer.cs says that _Fomp (Script) has not been instantiated!");
     }

     if (target != null) {
         distance = Vector3.Distance (transform.position, target.position);
         theAngle = Mathf.Asin ((9.81f * distance) / Mathf.Pow(myProjectileSpeed, 2.0f)) / 2.0f ;
 
         if (distance <= range) 
         {
             Vector3 newDirection = new Vector3(target.position.x,target.position.y + Mathf.Tan (theAngle) * distance , target.position.z) - transform.position;
             //Debug.Log ("Distance is: " + distance + " m" + " Corr Angle: " + theAngle + " target pos:" + target.position);
 
             if (newDirection != Vector3.zero)
             {
             targetRotation = Quaternion.LookRotation (newDirection, Vector3.up);
             transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRotation, aimRotationSpeed * Time.deltaTime);
             }
         }
     }
 }

 void GetTarget()
 {
     if (isTargetFound) // We do not have to searc, we have a target
     {
         return;
     }
     targetColliders = Physics.OverlapSphere (transform.position, range); // Find any collider in 100m range
     foreach (Collider hit in targetColliders) {
         if (hit.GetComponent<Rigidbody>() != null) 
         {

// if(hit.name != "Hamburger" && hit.name != "PunishMe" && hit.name != "Roof") // { // Debug.Log ("----->AutoAimer found these colliders in range: " + hit.name); // } if (hit.name.Contains("Player")) // Network instantiated player with a collider component in the transform. ( Not a specific empty with a collider) { //Debug.Log ("----->AutoAimer found a player in range." + hit.name); isTargetFound = true; target = hit.transform; } } } } }

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

94 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

Related Questions

Is there way to curve Raycast? 3 Answers

2D 360 degress platformer example needed 0 Answers

Rigid body Help Please 1 Answer

Help with optimize code 1 Answer

How do I solve the Box Collider (3D) Edge / Corner Collisions problem? 5 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