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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by elecs · Dec 26, 2013 at 06:40 PM · rotationrigidbodymovingaddrelativeforce

Problem with rigidbody rotation

hello friends, first of all I must tell you that I'm new to unity I am, I'm doing a script in C# that controls the movement of an Osprey i'm using a model with rigidbody I think is a more realistic alternative. I show a little code:

for elevation: rigidbody.velocity = new Vector3(0, elevation, 0);

for rotation: rigidbody.MoveRotation(Quaternion.Euler(ospreyEuler));

Now the problem arises here when I want to apply a force forward or backward: rigidbody.AddRelativeForce(transform.forward * verticalSpeed, ForceMode.Impulse);

once rotated model the forward vector object is not updated so that upon application of the force moves in what was forward before rotation ie if there is a rotation in the osprey 90 degrees to the right and apply the force for forward the osprey is moving to the left and vice versa.

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
0
Best Answer

Answer by elecs · Dec 26, 2013 at 11:13 PM

the error was in freeze position settings, had blocked the freeze position x the code was like this:

 // turning
 horizontalMovement = Input.GetAxis("Horizontal");
 ospreyPitch = Mathf.Lerp(ospreyPitch, -30 * horizontalMovement, Time.deltaTime);
 Vector3 ospreyEuler = rigidbody.rotation.eulerAngles;
 ospreyEuler.z = ospreyPitch;
         
 if (horizontalMovement > 0)
 {
     rotationSpeed = Mathf.Lerp(rotationSpeed, maxRotationSpeed, Time.deltaTime);
     ospreyEuler.y += Time.deltaTime * rotationSpeed;
 }
 else if (horizontalMovement < 0)
 {
     rotationSpeed = Mathf.Lerp(rotationSpeed, maxRotationSpeed, Time.deltaTime);
     ospreyEuler.y -= Time.deltaTime * rotationSpeed;
 }
 else
 {
     rotationSpeed = Mathf.Lerp(rotationSpeed, 0, Time.deltaTime *10f);
 }
         
 rigidbody.rotation = Quaternion.Euler(ospreyEuler);
 Vector3 targetVelocity = new Vector3(0, elevation, verticalSpeed);
 targetVelocity = transform.TransformDirection(targetVelocity);
 Vector3 velocity = rigidbody.velocity;
 Vector3 velocityChange = (targetVelocity - velocity);
         
 rigidbody.AddForce(velocityChange, ForceMode.Impulse);

thanks!

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
avatar image
1

Answer by robertbu · Dec 26, 2013 at 06:41 PM

You either want:

 rigidbody.AddRelativeForce(Vector3.forward * verticalSpeed, ForceMode.Impulse);

or:

 rigidbody.AddForce(transform.forward * verticalSpeed, ForceMode.Impulse);


'transform.forward' is a world coordinate, not a local coordinate.

Comment
Add comment · Show 6 · 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 elecs · Dec 26, 2013 at 06:46 PM 0
Share

yes, I have done so, and gives me the same result

avatar image robertbu · Dec 26, 2013 at 06:52 PM 0
Share

Are you rotating before you are applying force? You cannot apply force and then rotate.

avatar image elecs · Dec 26, 2013 at 08:49 PM 0
Share

do the rotation and then apply force

avatar image robertbu · Dec 26, 2013 at 08:59 PM 0
Share

You understand that applying new force does not cancel out the existing momentum of the object. If you have a ship in space moving forward, applying a side force turns the ship somewhat, but does not give it an immediate 90 degree turn. If this is the issue here, you can try a couple of things. First, you can up the Rigidbody.Drag(). Or for an immediate turn, you can directly assign to Rigidbody.velocity rather than use AddForce().

avatar image elecs · Dec 26, 2013 at 09:47 PM 0
Share

maybe the error is here?

 horizontal$$anonymous$$ovement = Input.GetAxis("Horizontal");
         osprey$$anonymous$$ch = $$anonymous$$athf.Lerp(osprey$$anonymous$$ch, -30 * horizontal$$anonymous$$ovement, Time.deltaTime);
         Vector3 ospreyEuler = rigidbody.rotation.eulerAngles;
         ospreyEuler.z = osprey$$anonymous$$ch;
         
         if (horizontal$$anonymous$$ovement > 0)
         {
             speedRotation = $$anonymous$$athf.Lerp(speedRotation , maxSpeedRotation , Time.deltaTime);
             ospreyEuler.y += Time.deltaTime * speedRotation;
         }
         else if (horizontal$$anonymous$$ovement < 0)
         {
             speedRotation = $$anonymous$$athf.Lerp(speedRotation , maxSpeedRotation , Time.deltaTime);
             ospreyEuler.y -= Time.deltaTime * speedRotation ;
         }
         else
         {
             speedRotation = $$anonymous$$athf.Lerp(speedRotation , 0, Time.deltaTime);
         }
     
     rigidbody.rotation =Quaternion.Euler (ospreyEuler);
Show more comments

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

18 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 avatar image

Related Questions

Problem when applying forces to a ball 1 Answer

Unprecise rotation when adding a relative force to a rigidbody 2 Answers

am I using degrees, radians, rotation or quaternion ? 2 Answers

Engine weight Simulation 2 Answers

Rotating sphere with rigidbody attached 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