Comments and answers for "find the exact point on which a projectile will hit enemy"
http://answers.unity.com/questions/1536516/find-the-exact-point-on-which-a-projectile-will-hi.html
The latest comments and answers for the question "find the exact point on which a projectile will hit enemy"Comment by notijaz on notijaz's answer
http://answers.unity.com/comments/1537349/view.html
Thanks i will check it out later.Thu, 02 Aug 2018 08:50:33 GMTnotijazComment by notijaz on notijaz's answer
http://answers.unity.com/comments/1537347/view.html
Superb. Thanks it will be enoughThu, 02 Aug 2018 08:48:52 GMTnotijazAnswer by ModLunar
http://answers.unity.com/answers/1537087/view.html
So um I don't have the time to explain everything about what I found, but here's the gist of it:
<br><br>
- 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²)
<br><br>
- 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.
<br><br>
- 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.
<br><br>
- 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.
<br><br>
- 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)
<br><br>
![alt text][1]
<br>
Highlighted in yellow are the final vector formulas.
<br><br>
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.
<br><br>
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
<br><br>
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
);
}
[1]: /storage/temp/121939-projectile-motion-with-addforce-forcemodeforce-163.jpgWed, 01 Aug 2018 16:46:26 GMTModLunarAnswer by coolbudy1998
http://answers.unity.com/answers/1537085/view.html
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!!Wed, 01 Aug 2018 16:40:19 GMTcoolbudy1998Comment by ModLunar on ModLunar's comment
http://answers.unity.com/comments/1537057/view.html
Oh not necessarily, I'll see if I can come up with a formula. It might get you a close approximation at leastWed, 01 Aug 2018 15:32:14 GMTModLunarComment by notijaz on notijaz's comment
http://answers.unity.com/comments/1536877/view.html
It means there is no way?Wed, 01 Aug 2018 09:27:21 GMTnotijazComment by ModLunar on ModLunar's answer
http://answers.unity.com/comments/1536717/view.html
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.
<br><br>
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 :(Tue, 31 Jul 2018 21:15:40 GMTModLunarComment by notijaz on notijaz's answer
http://answers.unity.com/comments/1536590/view.html
Any link or something?Tue, 31 Jul 2018 15:52:15 GMTnotijazAnswer by madks13
http://answers.unity.com/answers/1536573/view.html
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.Tue, 31 Jul 2018 15:29:15 GMTmadks13