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
0
Question by Thomas-Hawk · Jan 11, 2017 at 08:55 PM · scripting problemcollisionphysicsrigidbodycharacter movement

Having trouble turning a Transform movement into a Rigidbody force. Code included

I'm trying to create Arwing-like controls, in that my player is always moving forward relative to its rotation, and it can be rotate along any axis (Yaw, pitch, roll).

I have tried applying a rigidbody and converting my code (See the verbose commented out lines for my failures) yet, weird stuff would happen, like moving along the wrong axis, having to rotate things in my prefab to appropriate for inverted axis, crazy jazz. I really, really need collisions to happen though.

Here is my code:

 //rb.AddRelativeForce(Vector3.up * (speed + boostHolder));
         //rb.MovePosition(Vector3.forward * (speed + boostHolder));
         transform.Translate(forward * (speed + boostHolder));
         transform.Rotate(Input.GetAxis("Vertical")* turnspeed,  Input.GetAxis("Horizontal") * turnspeed, ((Input.GetAxis("Horizontal") / 3) * -1) + Input.GetAxis("Roll") * -turnspeed);
         //rb.AddTorque (Input.GetAxis("Vertical") * turnspeed,  Input.GetAxis("Horizontal") * -turnspeed,0);
         //transform.Rotate(vfloat * turnspeed,  Input.GetAxis("Horizontal") * turnspeed, ((Input.GetAxis("Horizontal") / 3) * -1) + Input.GetAxis("Roll") * -turnspeed);

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Scoutas · Jan 11, 2017 at 09:07 PM

The moving on a wrong axis happens, because Translate method of a tranform, moves the object in the Local Space of the object. So passing in Vector3.forward would move the object on it's forward axis. MovePosition from the rigidbody, the documentation says that you pass in the position you want your object to move to, using smoothing. I'd suggest using AddForce method, if you want to calculate the direction in the World Space of where the object has to go yourself, of AddRelativeForce if you want to just use Vector3.forward and make the object go on it's forward axis.

Same thing would happen with the addition of torque, but I myself have not played around with it too much, so I don't want to say something that's completely wrong.

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 Thomas-Hawk · Jan 11, 2017 at 11:56 PM 0
Share

Thank you, it's helpful.

I've completely rewritten my player movement and AI scripts to use rigidbodies ins$$anonymous$$d of directly translating the transform. Now they bounce off of the terrain, which is all I wanted, really.

But now the AI rotates like it is supposed to, but doesn't face the direction of movement / vice versa (Seems to have so much momentum / inertia that when it rotates or decides on new rotations / paths / targets, it just keeps traveling in the direction of that momentum -- even though it is applying forward force relative to its own rotation every frame.

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

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

Related Questions

Why is there no Rigidbody2D.SweepTest() ? 1 Answer

Add velocity relative to ground? 1 Answer

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers

physics culling mask? 1 Answer

Keep Horizontal Momentum after Jump 2 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