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
1
Question by Duncan Keller · May 25, 2013 at 05:33 PM · 2dphysicsrigidbodypathbezier

Implement 2D movement in a 3D environment while retaining physics

What I'm creating is essentially the movement system used in Kibry 64, where the player can move left and right along a 2D path, but the space itself is 3D.

What I have set up to implement this is a series of nodes that are connected by bezier curves. Setting up the path is not the issue, however. (I have an image below if anybody is having trouble picturing what I'm saying)

alt text

What I've been doing to keep the player along the path is to set the fwd vector to the tangent along the curve (derivative of the bezier function, with how far the player is along the curve plugged in). The problem with this is that if the player overshoots a node due to his speed, he will still only adhere to the curve's direction without being corrected onto the path. Needless to say he gets off base very quickly.

The other solution I thought of was to just keep track of 2D position in the script, and snap the player onto the curve. This will not work, though, because I am using a rigidbody for the player's physics model, and setting the position manually will break the physics.

Quite a pickle, eh? I know it's a bit of an unconventional problem, but I hope somebody will have the ingenuity and knowledge to set me in the right direction. Thanks!

example.png (18.5 kB)
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
1
Best Answer

Answer by aldonaletto · May 25, 2013 at 07:39 PM

You could try a simple P (proportional) controller: calculate the actual and the desired positions, and apply a force proportional to the error in order to fix the trajectory - something like this:

 var desiredPos: Vector3;
 var gain: float = 10.0;
 var maxForce: float = 50.0;
 
 function FixedUpdate(){
   // calculate the error
   var error: Vector3 = desiredPos - transform.position;
   error.y = 0; // remove Y component if you want to keep physics gravity
   // calculate the necessary force and clamp it to maxForce:
   var force: Vector3 = Vector3.ClampMagnitude(error * gain, maxForce);
   rigidbody.AddForce(force); // apply the force
 }

Update desiredPosition each frame and the controller will try to move the rigidbody to it. You may have to adjust the factors gain and maxForce in order to achieve a more stable operation, but oscillations and overshooting may occur in a simple P controller . A more complete approach (a PID controller) can be found in the Bonus Script of this question: it implements a Proportional Integral Differential controller.

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 Duncan Keller · May 25, 2013 at 07:51 PM 0
Share

Thanks aldonaletto, I'm going to give it a shot! I appreciate the help.

avatar image
0

Answer by Tomer-Barkan · May 25, 2013 at 07:36 PM

Can you calculate the world (3D) location as a function of the 1D distance from the beginning of the path?

Then you could simply advance "distance" as the player moves forward, and immediately calculate the position as

 transform.position = GetPositionFromDistance(distance);
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 Duncan Keller · May 25, 2013 at 07:39 PM 0
Share

Yeah, I had that thought too. The problem is that if I simply change the transform.position, it breaks the physics engine. I have to move everything by forces.

Thanks for the answer, though! I appreciate the help, it's a tough nut to crack.

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

15 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

Related Questions

Is there a solution to when colliders bypass? 2 Answers

Best way to move a rigidbody 1 Answer

Moving rigidbody along spline, and being able to use physics 1 Answer

[Complex] How to move the player similar to character controller while also applying rigidbody physics vectors 1 Answer

Unity 4.3 CharacterController collision with physics 2D not working 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