• 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
Question by Max 4 · Apr 08, 2011 at 07:49 AM · rotatedirectiontorque

Force and rotation

I'm making a flying script, but I'm quite new to Unity.

function Update () {
rigidbody.AddForce(10,0,0);
if( Input.GetButtonDown( "Horizontal" ) ){
    rigidbody.AddTorque(0,0,10);
    }
if( Input.GetButtonDown( "Vertical" ) ){
    rigidbody.AddTorque(0,10,0);
    }
}

Ok, first, I need to replace "rigidbody.AddForce" with something that makes the object go forward relative to its own direction, so it goes forward in the direction its facing.

Also, for the "AddTorque" part, how would I make it so that it only right when I press right, left when I press left, and so on. Currently, it can only rotate down and left, even when I press the opposite button.

I hope I'm not asking too much. ~Max

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by KeithK · Apr 08, 2011 at 08:10 AM

If you change AddForce(), to AddRelativeForce() it will apply the force on the axis relative to the objects coordinate system.

As for your Torque question, if you use Input.GetAxis("Vertical") you will get a value in the range -1..1. So -1 for left and 1 for right, 0 for no change.

You may also want to look at calling these functions in the FixedUpate() instead of Update().

var maxTorque : float = 1.0f; var forwardForce : float = 10.0f;

var currentTorqueHor : float = 0.0f; var currentTorqueVer : float = 0.0f;

function Update() { currentTorqueHor = maxTorque Input.GetAxis("Horizontal"); currentTorqueVer = maxTorque Input.GetAxis("Vertical"); }

function FixedUpdate() { rigidbody.AddForce(transform.forward * forwardForce); rigidbody.AddRelativeTorque(currentTorqueVer, 0.0f, -currentTorqueHor); }

EDIT: Note on AddForce() and AddRelativeForce()

Just thought I'd add a note quickly to explain why I mention AddRelativeForce() at the start of the post and use AddForce() in my script.

transform.forward returns the forward vector of the transform in world space, applying that vector relative to your objects rotating local coordinate system is going to yield some strange results.

You could use AddRelativeForce() if you used Vector3.forward to get the forward vector, as forward is forward to your object in it's local space no matter what. =P

Comment
Max 4
sushanta1991

People who like this

2 Show 7 · 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 Max 4 · Apr 08, 2011 at 08:28 AM 0
Share

It doesn't go the way it's facing. Also, I think that rotating right and left are inverse. Also, occasionally up and down make it rotate left and right while staying parallel with the ground.

Thans for the advice anyway.

avatar image KeithK · Apr 08, 2011 at 08:37 AM 0
Share

I think I have the wrong picture in my head of what you're doing. Is this a flying script for like an object controlled from first-person/third-person, or is this side scrolling?

avatar image Max 4 · Apr 08, 2011 at 08:41 AM 0
Share

It's a third person script following a plane over a terrain.

avatar image Max 4 · Apr 08, 2011 at 09:03 AM 0
Share

It's still quite glitchy. It doesn't rotate always and still usually goes one direction.

avatar image Max 4 · Apr 08, 2011 at 09:13 AM 0
Share

This is working really well! Is there anyway to inverse the right and left rotation, though? It still feels odd. But, anyway, thaks for the help!

Show more comments
avatar image

Answer by Bjorndal · May 02, 2011 at 03:24 PM

I have a question about Force and Rotation, so thought I would post it here.

I am using AddRelativeTorque to rotate a rigidbody around its own z-axis. The problem is it seems to rotate around the world axis instead. Here is my code, can someone see what I'm doing wrong here?

Roll =(Input.GetAxis("Roll"));

if(Roll != 0) { rigidbody.AddRelativeTorque (0,0, -1*Roll*RollR); }

Thanks

Comment

People who like this

0 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 Chris D · May 02, 2011 at 08:04 PM 0
Share

You're not likely to get an answer here; post it as its own question (but search for similar questions first).

avatar image Chris D · May 02, 2011 at 08:04 PM 0
Share

Also, please refrain from posting questions as answers in general.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

velocity direction 1 Answer

Unity2D, How do i check which way a sprite is facing? 1 Answer

Set rotation based on 2 points problem 1 Answer

(Picture for Clarity) How do you click on an object, and from that hit point, calculate a direction vector, and then make that direction vector be the upwards direction of an object? 0 Answers

How to rotate two axes independently, simultaneously 5 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