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
0
Question by matpneu · Sep 05, 2013 at 02:23 AM · rigidbodyplatformmovingparentingtrain

movement inside a train problem

Hello, i'm having a big problem with a moving platform, well it's not a platform it's a train, wich has this script.

        function OnTriggerEnter(col: Collider)
 {
     if(col.tag == "Player")
     {
        col.transform.parent = transform.parent;
       
     }
 }
 
 function OnTriggerExit(col: Collider)
 {
     if(col.tag == "Player")
     {
        col.transform.parent = null; 
     }
 }

this script just parents my player to my train when the player is inside the train, but since i have to use a rigidbody for my player so the game can detect collisions between a sword my character has and enemyes, when my characters gets out of the train he still continues to move with the train, so i don't know what to do, i think it would be nice to have a solution for moving platforms, or in my case trains, that does not require parenting in order to work, but i have no idea on how to use other kinds of solutions.

So anybody knows why my character continues to move when he's outside of the train?

or does anyone knows if there's a better solution on moving platforms, trains or vehicles other than parenting?

Comment
Add comment · Show 1
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 whydoidoit · Sep 05, 2013 at 05:10 AM 0
Share

Are you really using physics? If not you should make the rigidbody is$$anonymous$$inematic = true.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hoeloe · Sep 05, 2013 at 08:05 AM

There is a solution to this. You can add a momentum vector to your player, which is set whenever you touch the ground, and depends on the ground you touch. First of all, assign a rigidbody to the train (kinematic if you don't want actual physics on it). You can then raycast into the ground from your character, and if the character has a rigidbody, use rigidbody.GetPointVelocity(hit.point). This gets the current velocity of a specific point on the rigidbody collider. By setting the momentum value equal to this, you will find the player moves along with the train, and by resetting it when you land on other ground, you'll stop that momentum when the player leaves the train.

Comment
Add comment · Show 3 · 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 matpneu · Sep 08, 2013 at 02:57 AM 0
Share

Thank you for your answer, i kind of understand what you mean, but i have troubles trying to translate this to code, i just don't know how to use rigidbody.GetPointVelocity with a raycast, if you could tell me more about this and a little bit about the code, that would be great. Anyway, thanks for the answer.

avatar image RyanZimmerman87 · Sep 08, 2013 at 05:42 AM 0
Share

You may also be able to set up a Parent Transform Object which contains the train. After every FixedUpdate() frame you could calculate the distance the train traveled between the current frame and the last. Then you could apply this same distance transformation to the player depending on if he is grounded or not or whatever conditions you like?

I've never tried that for something so complicated like a train but it sounds like it should work in theory?

Also if the train moves in a perfectly straight line you could just apply it's current speed variable directly to the player?

Once again I have no idea if this stuff would work I've never done something like this just throwing out some initial thoughts.

avatar image Hoeloe · Sep 08, 2013 at 08:13 AM 0
Share

@RyanZimmerman87 That's basically the same method I suggested, except with a more accurate way of calculating the speed that would account for more complex motion (acceleration and rotation).

@matpneu Basically, you need a short raycast from the player to the ground. You can then do a quick test for if the ground has a rigidbody attached. If it doesn't, then set a momentum vector in the player to (0,0,0). If it does, then set the momentum vector to rigidbody.GetPointVelocity(hit.point), by using a RaycastHit object, and storing the output data. GetPointVelocity takes a world space coordinate, and returns a velocity vector of that point, according to the current rigidbody. You can easily get the rigidbody of the collider the raycast hit with hit.collider.rigidbody, so the code you actually need looks something like this:

 //Cast a ray and store the information in 'hit'
 RaycastHit hit;
 if(Physics.Raycast(transform.position, Vector3.down, out hit, 1))
 {
     //Set the momentum if the ground has a rigidbody
     if(hit.collider.rigidbody)
         momentum = hit.collider.rigidbody.GetPointVelocity(hit.point);
     else //Reset momentum if the ground doesn't have a rigidbody (i.e. isn't moving)
         momentum = Vector3.zero;
 }

You will also need to define a momentum vector manually, and each frame add it to the player's position.

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

19 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Something on a moving platform 2 Answers

Prevent Player to push Rigidbody 0 Answers

[C#] More elegant solution to making player move with moving platform? 1 Answer

Rigidbody on a platform 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