Unity make a missile follow a given trajectory
up vote
0
down vote
favorite
Im making a game where you can fire missiles at tanks. I want the missile to follow this trajectory:
https://imgur.com/a/bRQ44zq
I have tried a few things but no luck. Does anyone has an idea on how I could achieve this trajectory?
Thanks in advance for any help.
unity3d
add a comment |
up vote
0
down vote
favorite
Im making a game where you can fire missiles at tanks. I want the missile to follow this trajectory:
https://imgur.com/a/bRQ44zq
I have tried a few things but no luck. Does anyone has an idea on how I could achieve this trajectory?
Thanks in advance for any help.
unity3d
what are the axis stand for in your image?
– TheMri
Nov 14 at 14:32
Try usingVector3.Slerp
(there is example in API Refrenece), but you still need some calculations for center and arc angle.
– Morasiu
Nov 14 at 14:58
@TheMri the axis stand for x = distance and y = is height.
– jason devers
Nov 19 at 9:31
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im making a game where you can fire missiles at tanks. I want the missile to follow this trajectory:
https://imgur.com/a/bRQ44zq
I have tried a few things but no luck. Does anyone has an idea on how I could achieve this trajectory?
Thanks in advance for any help.
unity3d
Im making a game where you can fire missiles at tanks. I want the missile to follow this trajectory:
https://imgur.com/a/bRQ44zq
I have tried a few things but no luck. Does anyone has an idea on how I could achieve this trajectory?
Thanks in advance for any help.
unity3d
unity3d
asked Nov 14 at 14:29
jason devers
33
33
what are the axis stand for in your image?
– TheMri
Nov 14 at 14:32
Try usingVector3.Slerp
(there is example in API Refrenece), but you still need some calculations for center and arc angle.
– Morasiu
Nov 14 at 14:58
@TheMri the axis stand for x = distance and y = is height.
– jason devers
Nov 19 at 9:31
add a comment |
what are the axis stand for in your image?
– TheMri
Nov 14 at 14:32
Try usingVector3.Slerp
(there is example in API Refrenece), but you still need some calculations for center and arc angle.
– Morasiu
Nov 14 at 14:58
@TheMri the axis stand for x = distance and y = is height.
– jason devers
Nov 19 at 9:31
what are the axis stand for in your image?
– TheMri
Nov 14 at 14:32
what are the axis stand for in your image?
– TheMri
Nov 14 at 14:32
Try using
Vector3.Slerp
(there is example in API Refrenece), but you still need some calculations for center and arc angle.– Morasiu
Nov 14 at 14:58
Try using
Vector3.Slerp
(there is example in API Refrenece), but you still need some calculations for center and arc angle.– Morasiu
Nov 14 at 14:58
@TheMri the axis stand for x = distance and y = is height.
– jason devers
Nov 19 at 9:31
@TheMri the axis stand for x = distance and y = is height.
– jason devers
Nov 19 at 9:31
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You should search for: ballistics, cannon, ballistics physics, ball parabola, etc.
Here is an example of of shooting ball at transform:
https://answers.unity.com/questions/148399/shooting-a-cannonball.html
I don't have Unity open right now, so I can't give you full code. By the way, try to change the code below for your own use and let me know in comments.
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You should search for: ballistics, cannon, ballistics physics, ball parabola, etc.
Here is an example of of shooting ball at transform:
https://answers.unity.com/questions/148399/shooting-a-cannonball.html
I don't have Unity open right now, so I can't give you full code. By the way, try to change the code below for your own use and let me know in comments.
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
|
show 2 more comments
up vote
0
down vote
You should search for: ballistics, cannon, ballistics physics, ball parabola, etc.
Here is an example of of shooting ball at transform:
https://answers.unity.com/questions/148399/shooting-a-cannonball.html
I don't have Unity open right now, so I can't give you full code. By the way, try to change the code below for your own use and let me know in comments.
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
You should search for: ballistics, cannon, ballistics physics, ball parabola, etc.
Here is an example of of shooting ball at transform:
https://answers.unity.com/questions/148399/shooting-a-cannonball.html
I don't have Unity open right now, so I can't give you full code. By the way, try to change the code below for your own use and let me know in comments.
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
You should search for: ballistics, cannon, ballistics physics, ball parabola, etc.
Here is an example of of shooting ball at transform:
https://answers.unity.com/questions/148399/shooting-a-cannonball.html
I don't have Unity open right now, so I can't give you full code. By the way, try to change the code below for your own use and let me know in comments.
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
edited Nov 15 at 6:09
Eliasar
183111
183111
answered Nov 14 at 15:06
victor dabija
5069
5069
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
|
show 2 more comments
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
From their diagram it would seem that the OP wants to include the effects of air resistance.
– meowgoesthedog
Nov 14 at 15:46
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
well, in that case there is a magic page on wikipedia for ballistics. The code isn't changing so much. It have to consider rigidbody's drag if you want air resist.
– victor dabija
Nov 14 at 15:56
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
Even the simplest drag-inclusive case is significantly more complex than the drag-less one.
– meowgoesthedog
Nov 14 at 16:52
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
@meowgoesthedog i'm not getting your point (btw i really don't think the guy want air resistance)
– victor dabija
Nov 15 at 9:03
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
In vacuum the curves should be symmetric parabolas, but they are assymetric and steeper at the end, which corresponds to air resistance. The functional form for the case with air resistance is different.
– meowgoesthedog
Nov 15 at 9:07
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302551%2funity-make-a-missile-follow-a-given-trajectory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what are the axis stand for in your image?
– TheMri
Nov 14 at 14:32
Try using
Vector3.Slerp
(there is example in API Refrenece), but you still need some calculations for center and arc angle.– Morasiu
Nov 14 at 14:58
@TheMri the axis stand for x = distance and y = is height.
– jason devers
Nov 19 at 9:31