Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 this:

 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, which 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 this 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 this type of thing, 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

I suggest not using rigidbody physics at all when making projectiles

avatar image Tomer-Barkan · Oct 30, 2013 at 03:42 PM 0
Share

Here's a very simple script, with constant velocity that follows the target around:

 public Transform target;
 public float speed = 3f;
 private bool ifTarget = false;
  
 public void SetTarget(Transform helTarg){
     target = helTarg;
     ifTarget = true;
 }
  
 void Update () {
     if(ifTarget){
         Vector3 toTarget = target.position - transform.position;
         rigidbody.velocity = toTarget.normalized * speed;
  
         transform.forward = toTarget.normalized;
     }
 }

I simply set the velocity to be towards the target, and its magniuted to be speed (which is 3). I also rotate the missile every frame to make sure it's pointing at the target.

Note, for this to work, the missile's forward must be the missile's head. If the missile's head is pointing upwards ins$$anonymous$$d of forward, use the following line ins$$anonymous$$d of transform.forward =:

 transform.up = toTarget.normalized;
avatar image Kennyist Tomer-Barkan · Oct 30, 2013 at 03:48 PM 0
Share

oh sorry, forgot about this question. I came up with this in the end:

 public class Hellfire : $$anonymous$$onoBehaviour {
     
     private Transform target;
     private float force = 2f;
     private bool ifTarget = false;
     private float starttime;
     
     public void SetTarget(Transform helTarg){
         target = helTarg;
         ifTarget = true;
         starttime = Time.time + 0.5f;
         transform.parent = null;
         gameObject.AddComponent<Rigidbody>();
     }
 
     void FixedUpdate () {
         if(ifTarget){
             if(target == null){
                 rigidbody.velocity = (transform.forward * 1500 * Time.deltaTime);
                 
             } else {
                 
                 if(Time.time > starttime){
                     Vector3 dir = target.position - transform.position;
                     Quaternion rot = Quaternion.LookRotation(dir);
                     transform.rotation = Quaternion.Slerp(transform.rotation, rot, force * Time.deltaTime);
                 }
                 
                 rigidbody.velocity = (transform.forward * 1500 * Time.deltaTime);
                 
             }            
         }
     }
 }

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 this 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 something 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', this 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

If you add (in the second solution) some drag to the missile, you will get a lot better accuracy. You will also have to add more speed to the missile.

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.

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

Issue's With Positioning 0 Answers

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


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