• 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 Kennyist · Oct 20, 2013 at 11:49 PM · c#rigidbodytransformpositionvector3

Guided missile help c#

Basically im trying to make a physics based guided missile script, so far i have t$$anonymous$$s:

 public Transform target;
 public float force = 0.5f;
 private bool ifTarget = false;
 
 public void SetTarget(Transform helTarg){
     target = helTarg;
     ifTarget = true;
 }

 void Update () {
     
     if(ifTarget){
         
         rigidbody.AddForce(transform.up * 100);
 
         Vector3 targetDelta = target.position - transform.position;
         
         //get the angle between transform.forward and target delta
         float angleDiff = Vector3.Angle(transform.forward, targetDelta);
                            
         // get its cross product, w$$anonymous$$ch is the axis of rotation to
         // get from one vector to the other
         Vector3 cross = Vector3.Cross(transform.forward, targetDelta);
  
         // apply torque along that axis according to the magnitude of the angle.
         rigidbody.AddTorque(cross * angleDiff * force);
         
     }
 }

But t$$anonymous$$s only causes weird behaviour with the missile, as seen here:
http://tristanjc.com/unity/newweb.html (Target a tank with Tab and Press F for HellFire missiles, then click)

How would you go about a proper or a simple way to do t$$anonymous$$s type of t$$anonymous$$ng, also how would you make it go towards a target without using addForce, as that increases the speed at the same time.

Comment
Add comment · Show 3
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 · Oct 21, 2013 at 12:29 AM 0
Share
avatar image Tomer-Barkan · Oct 30, 2013 at 03:42 PM 0
Share
avatar image Kennyist Tomer-Barkan · Oct 30, 2013 at 03:48 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by robertbu · Oct 21, 2013 at 05:10 AM

What to use will partly depend on your functionality. For example, I'd pick a different solution depending on whether you want to allow the missile to miss the target or not. For a non-miss you can use:

 transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
 transform.LookAt(target.position);

If you want to use a Rigidbody, I'd try t$$anonymous$$s first:

  • Make sure your missile is constructed so that the nose of the missile is pointed to forward. That is, when the rotation is (0,0,0), the nose should be looking at positive 'Z'.

  • In the inspector increase the Drag significantly.

Then try somet$$anonymous$$ng like the following (executed in FixedUpdate()):

 rigidbody.AddForce((target.position - transform.position)*amount);
 transform.rotation = Quaternion.LookRotation(rigidbody.velocity);

Depending on the setting of 'Drag', t$$anonymous$$s will act fairly realistically if the target is moved. Here is an alternate couple of lines that makes the game object perfectly track the target:

 rigidbody.velocity = ((target.position - transform.position) * speed);
 transform.LookAt(target.position);
Comment
Add comment · Show 1 · 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 LeHombreDeZbragl · Mar 02, 2021 at 05:33 PM 0
Share

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

How to store position in a variable and than edit only specific part of it(such as 'x') 1 Answer

How to rotate an object to face the direction it's going? 1 Answer

Trouble setting up a vector 3. 1 Answer

Script to make Camera follow the player C# 3 Answers

Issue's With Positioning 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges