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
2
Question by PuffyChair · Feb 09, 2015 at 08:19 PM · physicsaddforcepathspline

Add force along a path

Hi!

I can't seem to figure out a way to push an object along a path using AddForce. My goal is to only limit the players x axis along a spline, and keep the y and z axis free for jumping and going forward/backwards using AddForce.

I've seen some good examples out there like itween and spline walker, but these only reposition the object, and does not use physics to create movement.

Does anyone know how to achieve this effect?

EDIT: During my testing I've made use of a script that I found on this site: http://catlikecoding.com/unity/tutorials/curves-and-splines/ Maybe I can make this work if I can calculate the closest point to the player on the Bezier Spline. Does anyone know what piece of code I would need for this?

Thanks!

Comment
Add comment · Show 6
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 AlwaysSunny · Feb 09, 2015 at 06:37 PM 0
Share

I'm making several assumptions about the nature of your game, but I don't think you'll get the behavior you're expecting. The best your approach can do will produce undesirable effects when your body interacts with others, and you will not follow the spline directly; it'll be more of a suggestion, like: Seek this X position by adding force on X whose magnitude is affected by current distance from desired X. This isn't technically spline-following, nor will it produce guaranteed-reasonable results, especially when other bodies are involved in the simulation.

Tasks like this aren't necessarily suited to the physics system. (You could still use the system to check for collisions, but drive motion without physics and use a kinematic rigidbody).

Don't let me talk you out of it, though. It's worth a try and might meet your needs depending on what you expect!

Anyone care to challenge my wisdom here? I always enjoy being proven silly or just plain wrong.

avatar image PuffyChair · Feb 09, 2015 at 08:26 PM 0
Share

Hi Sunny!

Thanks for your feedback! Sorry if I'm not understanding you correctly, but I don't want to seek x by applying force. I mean that I want the x axis to always be locked to the spline, but affect y and z by force. $$anonymous$$ind of like the behavior you get from freezing rotation or position on a rigidbody; You can collide with other objects, but limited to the locked axis. I'll attach an image if that describes my vision clearer. :)

alt text

add-force-with-spline.jpg (45.7 kB)
avatar image AlwaysSunny · Feb 09, 2015 at 08:39 PM 0
Share

Ah, my apologies if I misread; I definitely misunderstood. Now I understand that you mean the body's local X relative to the spline is clamped. I dig your ALLCAPS script; I do that too, especially w/ a tablet.

This question just got interesting. No trivial-yet-ironclad approach springs to $$anonymous$$d, but I'm along for the ride.

I assume because you're wanting to use forces to drive some aspects of motion, you want this body to physically interact with some part of the world, right? What should happen when this body collides with another? What types of collision events do you expect?

What kind of data can you pull from the spline? Can you, for instance, sample two nearly-adjacent positions A & B to find the forward direction with (B-A).normalized?

avatar image AlwaysSunny · Feb 09, 2015 at 08:53 PM 0
Share

As I think on this, I'm wondering if there actually is a "good" way to make this happen. The physics simulation is always in world space, and you're essentially wanting it to work in object space.

I guess you could find the object's local X direction each FixedUpdate and nullify all velocity in that direction, but I'm not sure how that would behave when the body interacts with another. At best I think it would force the other object aside in a physically unrealistic manner and compromise the purity of the simulation on the two unclamped axes.

avatar image AlwaysSunny · Feb 10, 2015 at 01:21 PM 1
Share

"Or maybe we can get away with constantly altering the velocity in the direction of the spline at the closest point?"

I believe that's as good as you're going to do without breaking away from the physics simulation altogether. Consider though: Velocity changes always necessarily disrupt the purity of the physics simulation, meaning the forces you apply are not the forces you will observe in practice. If you are applying forces and changing velocities every frame, you are not, for all intents and purposes, using the physics simulation as intended. It can also cause issues when the body interacts with another.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by nirharpaz · Feb 10, 2015 at 08:35 AM

I think I have an idea how I would approach that.

every frame the obj has a position, he calculate his position comparing to the spline and move another step. so if i look of each step.. there is the middle of the object and the middle of the spline. if the spline is straight the middles are merged and you move forward. in fact you always move forward. diff is when the middles that you are handling to next step are not merged - same direction and position. in this case I would calculate the difference in rotation and movement and add force and rotation towards potting the obj middle on the spline middle.

that's my rough idea

alt text


untitled.png (2.9 kB)
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Update speed and physics makes my rigidbody jiggle 2 Answers

AddForce forward also adding downward force 0 Answers

move player to its rotating direction? 2 Answers

AddForce in Update 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