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
1
Question by notijaz · Jul 31, 2018 at 01:45 PM · enemyprojectilehitcalculationmotion-tracking

find the exact point on which a projectile will hit enemy

Hey everybody. I was just wondering if there is anyway to find the exact point on which a projectile will hit the enemy (head, lung, or other parts). In my game the enemies are far far away so the player has to aim above the enemy to hit him. I,m using rigidbody and addforce for my projectile motion, and plus raycast is not possible as raycast move in straight line. I want to find it as soon as the projectile is launch or even before it actually hit the enemy. Thanks in advance

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ModLunar · Aug 01, 2018 at 04:46 PM

So um I don't have the time to explain everything about what I found, but here's the gist of it:

- I assumed we're on a big enough planet to approximate that gravity only acts in the vertical y direction. (You can get the gravity vector in Unity as Physics.gravity, and Physics.gravity.y is by default -9.81 (m/s²)

- We're dealing with projectile motion. In a physics class, this would be under a section usually called "Kinematics", with no regard to forces -- just displacement, velocity, and acceleration.

- This was (for me) a 2-step problem. First, "translating" the force applied to the object into what velocity that force would cause. Then, once I calculated the initial velocity, caused by the force (assuming the force happened nearly instantaneously, with only one call to rigidbody.AddForce(...)), I used kinematics equations to figure out its position after a given amount of time, based on that velocity it started with, and that gravity will be affecting (only) the y direction.

- If it doesn't help you because time isn't what you want to be plugging in these equations (maybe you don't care about the time it takes to do this) you'll have to take these formulas a step further for what you wanna do, and perhaps use a couple of raycasts to actually see if it'd collide with anything along its path.

- The first part where I had to get the initial velocity of the object based on the force you applied -- this comes from Newton's Second Law (F=ma)

alt text
Highlighted in yellow are the final vector formulas.

The most important one you want is Δr(t), which represents the change in position of the object over a certain time t in seconds after the launch. Gah I forgot the vector symbol above the r! Lol :( oh well. And v(t) represents the object's velocity over that same time period. t = 0 when you just started the launch. t = 1 would be one second after the launch. These formulas are only valid until it hits something, because the entire problem, including its trajectory, changes after that.

Any details about what this thing would be colliding with, you'd use some kind of raycast, spherecast, boxcast, etc. from Unity's UnityEngine.Physics class. These formulas (most notably Δr(t)) will help give you the path the object will travel. Here's some quick and dirty code I wrote out, I did it in Notepad++ so hopefully I didn't make any silly typos that'd make it not compile xD

 private Rigidbody rigidbody;
 private Vector3 initialPos;
 private Vector3 initialVelocity;
 
 private float startTime;
 
 public void Start() {
     projectile = GetComponent<Rigidbody>();
 
     LaunchProjectile(new Vector3(500, 0, 0));
 }
 
 private void LaunchProjetile(Vector3 force) {
     initialPos = projectile.position;
     initialVelocity = force * Time.fixedDeltaTime / projectile.mass;
     startTime = Time.time;
     
     //AddForce(...) without the second parameter for the ForceMode
     //means you used ForceMode.Force, which is the default
     projectile.AddForce(force);
     
     //Actually you might be able to just get the initialVelocity here with projectile.velocity... lol. But I'm not sure if its velocity gets calculated immediately... if anything, wait one FixedUpdate frame.
 }
 
 public void Update() {
     float timeAfterwards = Time.time - startTime;
     Debug.Log("Theoretical position at t = " + timeAfterwards + ": " + GetPositionAfterTime(timeAfterwards, initialPos, initialVelocity);
 }
 
 //time is in seconds
 private Vector3 GetPositionAfterTime(float time, Vector3 initialPos, Vector3 initialVelocity) {
     return new Vector3(
         initialVelocity.x * time,
         0.5f * Physics.gravity.y * time * time + initialVelocity.y * time,
         initialVelocity.z * time
     );
 }


projectile-motion-with-addforce-forcemodeforce-163.jpg (424.0 kB)
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 notijaz · Aug 02, 2018 at 08:48 AM 1
Share

Superb. Thanks it will be enough

avatar image
-4

Answer by madks13 · Jul 31, 2018 at 03:29 PM

Yes there is, it's called math. I don't have the formula, but you should be able to find asset that will help you do that. Otherwise you will have to implement the formula yourself.

Comment
Add comment · Show 4 · 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 notijaz · Jul 31, 2018 at 03:52 PM 0
Share

Any link or something?

avatar image ModLunar · Jul 31, 2018 at 09:15 PM 1
Share

I know what you're trying to say but sometimes those formulas can be challenging to derive yourself, and even then, they can be physically correct (accurate, just as you learned in your physics class) but still deviate from the result that the physics engine comes up with, and you won't even know why.

I've tried to pre-calculate the max height that my character will jump given the constant acceleration of gravity in Physics.gravity and my initial y-velocity, and my calculations are always a little off and I don't know why :(

avatar image notijaz ModLunar · Aug 01, 2018 at 09:27 AM 0
Share

It means there is no way?

avatar image ModLunar notijaz · Aug 01, 2018 at 03:32 PM 0
Share

Oh not necessarily, I'll see if I can come up with a formula. It might get you a close approximation at least

avatar image
0

Answer by coolbudy1998 · Aug 01, 2018 at 04:40 PM

Hey bro, you can check out this link- http://www.theappguruz.com/blog/display-projectile-trajectory-path-in-unity. The bottom part of code shows a formula for velocity and angle of projectile (these two are the main characteristic feature of a projectile). Understand it thoroughly and you are done!!

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 notijaz · Aug 02, 2018 at 08:50 AM 0
Share

Thanks i will check it out later.

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

90 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

Related Questions

Make enemy Flash when Hit 3 Answers

How can I acces the script of an object that I triggered? 1 Answer

Hit enemy life 0 Answers

How do I get my enemy to shoot my player? 1 Answer

How do I apply bullet projectile to enemy ai properly on unity3d 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