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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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
















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












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);
}
}





share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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);
}
}





share|improve this answer























  • 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

















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);
}
}





share|improve this answer























  • 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















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);
}
}





share|improve this answer














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);
}
}






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?