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 Bob5602 · Jan 01, 2011 at 04:29 PM · rotationphysicslookattargetingaddrelativetorque

LookAt but using physics AddRelativeTorque

Hello all!

I posted a similar question before but did not get a response that really answered the question. I think its because it was badly worded, so here is another shot :

I am looking for a way to use the rigidbody physics engine to turn an object so that "forward" is facing a target object. I know I can do this easily via the LookAt function, but I want it to be completely physics based. Here is what I have thus far :

function TurnRight(){ if(torqueRight < 20){rigidbody.AddRelativeTorque(Vector3.up * turnPower); torqueRight ++;}} function TurnLeft(){ if(torqueRight >-20){rigidbody.AddRelativeTorque(Vector3.up * -turnPower); torqueRight --;}}

function TurnUp(){ if(torqueUp < 20){ rigidbody.AddRelativeTorque(Vector3.right -turnPower); torqueUp ++;}} function TurnDown(){ if(torqueUp > -20){ rigidbody.AddRelativeTorque(Vector3.right turnPower); torqueUp --;}}

function SpinRight(){ if(spin < 5){rigidbody.AddRelativeTorque(Vector3.forward -turnPower .25); spin ++;}} function SpinLeft(){ if(spin >-5){rigidbody.AddRelativeTorque(Vector3.forward turnPower .25); spin --;}}

function StabalizeUp(){ while(torqueUp > 0){ TurnDown(); } while(torqueUp < 0){ TurnUp(); } } function StabalizeRight(){ while(torqueRight > 0){ TurnLeft(); } while(torqueRight < 0){ TurnRight(); } } function StabalizeSpin(){ while(spin > 0){ SpinLeft(); } while(spin < 0){ SpinRight(); } }

With that code, I am able to turn my object using phyiscs any given way. Up, Down, Left, Right, and spin, are all represented. In addition, I have some functions that stabilize the rotation at any given moment.

Now, what I am looking for, is a way to "aim" using these functions, toward an object. I am currently using this :

function Aim(){ if(aimX > 0.01){ TurnRight(); //SpinRight(); } if(aimX < 0.01){ TurnLeft(); //SpinLeft(); } if(aimX > -0.01 && aimX < 0.01){ StabalizeRight(); //StabalizeSpin(); }

 if(aimY &gt; 0.01){
 TurnDown();
 }   if(aimY &lt; 0.01){
     TurnUp();
     }   if(aimY &gt; -0.01 &amp;&amp; aimY &lt; 0.01){
         StabalizeUp();
         }

}

where the aimY and aimX come from :

fwd = transform.forward;

var testAim : ShipTargeting = aimAssist.GetComponent("ShipTargeting"); aim = testAim.fwd;

aimX = fwd.x - aim.x; aimY = fwd.y - aim.y;

and what I am doing is I have a gameobject, aimAssist, attached to the rotating object. aimAssist is using LookAt to aim at the target.

However, my ship is still not lining up properly. Is there a better way to figure out how to add the torque of my rotation functions so that the object looks toward the target?

Comment
Add comment · Show 2
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 DSebJ · Feb 23, 2014 at 01:04 AM 0
Share

Did you come up with a solution to this?

avatar image M4R5 · Jul 31, 2015 at 06:20 PM 0
Share

I'm struggling with the same problem :/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hypnoslave · Jan 01, 2011 at 07:01 PM

I don't mean to deconstruct, but what are you trying to accomplish here? why does this need to be all done with physics?

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 Bob5602 · Jan 01, 2011 at 07:44 PM 0
Share

Well, this is all for the AI of space ships for a turret defense style game I am making. Since these ships are going to be moving around using thrust, I want them to turn using thrust. If I don't do this all with physics, the rigidbody velocity measurements get skewed and my turrets lose all accuracy. Plus, it doesn't have that authentic space 3d feel without using physics, otherwise it becomes jerky movement and unrealistic.

avatar image PrimeDerektive · Jan 01, 2011 at 08:53 PM 0
Share

I'm not sure how it would be done, but I assure you that it would look exactly the same (and just as smooth) as slerping the rotation to the target, and the slerp would definitely perform better.

avatar image Bob5602 · Jan 03, 2011 at 02:45 PM 0
Share

What about velocity? If I am changing the rotation with slerp, and adding "forward" thrust using physics, won't my rigidbody velocity measurement be screwed up?

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

2 People are following this question.

avatar image avatar image

Related Questions

[Solved] Getting relative torque from mass and set turn rate? 1 Answer

Rotating a Rigidbody with Physics 1 Answer

Simulating the graphics of a rolling 3D ball in a 2D game 2 Answers

How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer

Keep rotation when changing parent in Unity 3D 1 Answer


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