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 answers for the question "find the exact point on which a projectile will hit enemy"Answer 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 GMTcoolbudy1998Answer 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