Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by dtoliaferro · Aug 29, 2011 at 07:27 AM · rigidbodymissileaddrelativetorque

Physics-based Homing Missile

How can I calculate the amount of seconds needed, at a certain force, to rotate an object to face another object with AddRelativeTorque?

... I think that's my question.

Basically, I want to make a physics-based homing missile.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by dtoliaferro · Sep 02, 2011 at 11:28 AM

Got it. duckets from the IRC helped me with it. Basically, it's a torque based lookat script.

using UnityEngine; using System.Collections;

// @robotduck 2011 // set the object's rigidbody angular drag to a high value, like 10

public class TorqueLookAt : MonoBehaviour {

 public Transform target;
 public float force = 0.1f;

 
 // Update is called once per frame
 void Update () {
 
     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);
 
     
 }

}

Comment
Add comment · Show 2 · 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 SisterKy · Sep 02, 2011 at 01:35 PM 0
Share

thanks for sharing the answer with the rest of us :) please select your answer as accepted.

avatar image dtoliaferro · Sep 02, 2011 at 02:02 PM 0
Share

No problem. Thanks for the tip. I'm kinda new to this.

avatar image
1

Answer by Peelark · Apr 04, 2015 at 04:21 PM

This thread is ancient, but I came across it looking for a way to make this work in Unity 2D for a full physics-based space shooter. I want the enemy ships to track my player.

The issue in 2D is that you can't use a Vector3 to apply torque, so your angle as a Vector2 loses the desired rotation direction - i.e., your enemy always turns the same way. The way I fixed this was to take the normalised cross product and multiply the final float by its sign as follows (note the use of "up" because of the way axes are oriented in Unity2D:

 float enemyTurnTorque = 1f;
 
 //find the object you want to track - in my case "player"
 Vector3 targetDelta = player.position - transform.position;
 
 //get the angle between transform.forward and target delta
         float angleDiff = Vector3.Angle(transform.up, targetDelta);
 
 //get the cross product to determine which way to turn
         Vector3 cross = Vector3.Cross(transform.up, targetDelta);
 //normalise it and create a float, to hold a -1 or 1 corresponding to the right direction
         cross.Normalize ();
         float turnDirection = cross.z;
 
 // apply torque along that axis according to the magnitude of the angle.
         rb.AddTorque(enemyTurnTorque * turnDirection);
Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

ExplosionForce On Instantiated Object 1 Answer

Rotation and moving object simultaneously problem? 0 Answers

Bullet always facing the sky. 3 Answers

AddTorque is spinning faster in one direction than the other.. 1 Answer

Using Rigidbody AddRelativeTorque on a planetary surface 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