- Home /
I Need Help Building A Rocket/Missile That Hits A Target on The Other Side of a Planet
Greetings Everyone,
I need help building a missile or rocket that has the potential of hitting a t\arget anywhere on a planet, but the part where hitting the target on the other side of the planet is very difficult
(I have 2 months of coding exp. with c# not much with javascript) This is what i used, but i can't get the missile to hit the target on the other side without it leaving completely out of view and coming back a lot later
#pragma strict
var missileVelocity : float = 300;
var turn : float = 20;
var homingMissile : Rigidbody;
var fuseDelay : float;
var missileMod : GameObject;
var smokePrefab : ParticleSystem;
var missileClip : AudioClip;
var force : Vector3; //variable for "lift" of the rocket
var relativeTorque : Vector3; //variable for torque (spin) of rocket
var liftSpeed : float; //determines how much force will be applied
var turnSpeed : float; //determines how much torque will be appled
private var target : Transform;
function Start()
{
homingMissile = transform.rigidbody;
Fire();
}
function Fire()
{
yield WaitForSeconds(fuseDelay);
AudioSource.PlayClipAtPoint(missileClip, transform.position);
var distance = Mathf.Infinity;
for (var go : GameObject in GameObject.FindGameObjectsWithTag("target"))
{
var diff = (go.transform.position - transform.position).sqrMagnitude;
if(diff < distance)
{
distance = diff;
target = go.transform;
constantForce.force = Vector3.up * liftSpeed; //determines the "lift"
constantForce.relativeTorque= Vector3(0,turnSpeed,0);
}
}
}
function FixedUpdate()
{
if(target == null || homingMissile == null)
return;
homingMissile.velocity = transform.forward * missileVelocity;
var targetRotation = Quaternion.LookRotation(target.position - transform.position);
homingMissile.MoveRotation(Quaternion.RotateTowards(transform.rotation, targetRotation, turn));
}
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name == "Cube")
{
smokePrefab.emissionRate = 0.0f;
Destroy(missileMod.gameObject);
yield WaitForSeconds(5);
Destroy(gameObject);
}
}
Please can anyone help me with a solution, Thank you.
Notes: now i tried every way possible with the code i have, but i can't achieve it. (some Ideas) I also tried adding gravity to the planet but it messes the targeting of the rocket, I thought about using a straight plane and adding a shader to curve it into a planet, but I don't know how. I have used way points but it doesn't look right, the rocket points else where.
I wouldn't be surprised if something like the NSA flagged this post for potential terrorism. Just read the title out loud and you get what I mean XD
Anyway, you should indent your code. I can't be asked to fix it up for you. If you want people to work with your code, you better make it readable.
Answer by marcfielding · Feb 18, 2014 at 12:12 PM
Why don't you just attach a NavMesh to your planet and have the rocket follow that? I'm pretty sure the rocket doesn't actually have to be touching the NavMesh to use it. If it does have an empty GameObject on the surface, child your rocket and then offset it to be above it?
Sorry Ben, that codes perfectly readable without an indent.
EDIT
Oh if you don't have Unity Pro(I think its still required for NavMesh Agents) i'd recommend RAIN from Rival theory, its quite simply awesome! You can get it here
Good Luck!
Marc
EDIT It seems Rain won't work on a sphere so here's another suggestion:
Place an empty game object at the center of your planet launch the missile when it gets high enough parent it to the empty game object and then rotate the gameobject, the missle will remain at the right distance, you could then use a raycast at the surface until you detect your target then just fly the missle right at it?
Sorry for responding late but thank you so much for introducing me to RAIN, but i cant get a navmesh to work on a sphere and neither my planet, any tips on that
This is not an answer to the question. Please post it in the comments section.
Hrm I didnt think to check that no it won't work on a sphere it seems
I've edited my answer with another suggestion
Your answer
![](https://koobas.hobune.stream/wayback/20220613133901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make thruster rotation 0 Answers
missile homing script help 1 Answer
Missiles move to already destroy objects [Problem][Update] 4 Answers