• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Legenetic2 · Feb 18, 2014 at 03:52 AM · physicsplanetmissilerockethoming

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

alt text

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

cannons.jpg (98.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Feb 18, 2014 at 11:00 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Legenetic2 · Feb 20, 2014 at 05:04 AM 0
Share

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

avatar image Benproductions1 Legenetic2 · Feb 21, 2014 at 06:27 AM 0
Share

This is not an answer to the question. Please post it in the comments section.

avatar image marcfielding · Feb 22, 2014 at 01:20 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

missile homing script help 1 Answer

Move missile to nearest target 1 Answer

Missiles move to already destroy objects [Problem][Update] 4 Answers

Planetary based gravity 0 Answers

Angle between player and rocket targetting player. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges