Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
5
Question by CracksisT · Feb 24, 2010 at 10:28 PM · collisionphysicscharacterplatformmove-with-moving-platform

How to get a Character to move with a moving platform ?

I've been trying to get my character to move along with my moving platform (like when i jump unto the platform it carries me long with it), but so far have been unable to do so.
I have a character with CharacterController, it has also been tagged by 'Player'.
My moving platform has a 'Rigidbody' attached to it, a 'Configurable Joint'and a 'Box Collider'. The platform is tagged with 'JumpPad5'.
This is the script attached to my moving platform:

var targetA : GameObject;
var targetB : GameObject;

var speed : float = 0.1;

function FixedUpdate ()
{
var weight = Mathf.Cos(Time.time speed 2 Mathf.PI) 0.5 + 0.5; transform.position = targetA.transform.position weight + targetB.transform.position (1-weight);
}

So if anyone can help me, pleaso do.

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

6 Replies

· Add your reply
  • Sort: 
avatar image
16

Answer by superventure · Nov 05, 2011 at 07:37 PM

Can't you make the player a child to the platform with On trigger stay/exit events? That way the player will move, rotate, etc with the platforms movements AND speed, while still having their own controls.

  function OnTriggerStay(other:Collider){
             
             if(other.gameObject.tag == "platform"){
             transform.parent = other.transform;
 
         }
     }
 
 function OnTriggerExit(other:Collider){
     if(other.gameObject.tag == "platform"){
             transform.parent = null;
             
         }
     }    
Comment
Add comment · Show 8 · 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 Tekksin · Oct 30, 2013 at 05:25 PM 1
Share

that was a lot of help, thanks!

Quick question, though. I only want the player to ride the platform. The scale on the platform is different, and it changes the size of the player. Is there a way to make it so that the player doesn't change size and ins$$anonymous$$d just is parented via only the position?

avatar image fjalla · Oct 30, 2013 at 07:22 PM 1
Share

well you can set only the position and rotation manually like this:

 transform.position = other.transform.position;
 transform.rotation = other.transform.rotation;

EDIT: AND you don't need an OnTriggerExit, BUT you can't move while on the platform. The best thing you can do is to put the graphics of the platform into a child, and on the parent change the size of the collider accordingly to the graphics, and use the code by @superventure

avatar image Dracorat · Oct 30, 2013 at 08:01 PM 5
Share

This is what happens when you don't use Empty objects.

The right way to implement the platform would be:

  • Platform (Empty)

  • Platform's $$anonymous$$esh (Scaled and possibly with attached collider objects here)

  • Player (Parented to the Empty which isn't scaled)

The Empty has the attached scripts and is what you modify the transform and rotate properties of.

avatar image Tekksin · Oct 30, 2013 at 08:29 PM 0
Share

the platform empty, with a platform in it didn't work for me. I even went into the code attached to the player and wrote "if(theCollision.gameObject.name == "_Empty"){

An empty isn't a collision, and it doesn't have a trigger option either so..... i didn't expect it to work..

and I tried what you said, Doctor Jellyface (lol), but I got an error. this is what the code looks like:

 function OnCollisionEnter(theCollision : Collision){
 
     if(theCollision.gameObject.name == "$$anonymous$$over"){
     transform.position = other.transform.position;
     }
 }

and I get the error: Assets/Scripts/Player.js(68,43): BCE0019: 'position' is not a member of 'UnityEngine.Collision'.

I have since found a workaround to my problem, but it would still be nice to know of a proper way of doing things.

avatar image Dracorat · Oct 30, 2013 at 08:37 PM 0
Share

The collision would still happen in the mesh, but you'd attach movement and the player to the Empty.

Show more comments
avatar image
10

Answer by Mahtub · Dec 22, 2016 at 04:30 PM

i have a simple way.
1.add box collider to your platform and make sure that isTrigger is enabled
2.create new c# script
3.attach the c# script to the platform you want
4.copy script below to your c# script that you made

 private GameObject target=null;
 private Vector3 offset;

 void Start(){
     target = null;
 }

 void OnTriggerStay2D(Collider2D col){
     target = col.gameObject;
     offset = target.transform.position - transform.position;
 }
 void OnTriggerExit2D(Collider2D col){
     target = null;
 }

 void LateUpdate(){
     if (target != null) {
         target.transform.position = transform.position+offset;
     }

 }


good Luck

first share is here http://answers.unity3d.com/answers/1289185/view.html

Comment
Add comment · Show 4 · 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 MateoGallardo · Nov 02, 2017 at 12:14 AM 0
Share

Finally a solution that worked! Thanks! Every other solution changed the scale of my player when it was affected by gravity.

avatar image lod_ashtaroth · Dec 06, 2017 at 06:27 PM 1
Share

$$anonymous$$y character is still just falling through the platform

avatar image PrettyLyn · Oct 23, 2018 at 03:07 AM 2
Share

This is exactly what I needed thanks! but I had a problem when my player get inside the platform and tried to move it outside the platform again it doesnt move.. how to fix? your response is much appreciated..

avatar image DoomPriestK · Dec 03, 2019 at 11:30 PM 0
Share

Just worked for me! A*!

avatar image
1

Answer by dsamuelson · Aug 06, 2015 at 03:35 PM

I've been working on this for 2 days and finally found a way so that the character can stay on the platform and then be able to move around and continue to stay on the platform.

The principle is simple you want it so that when you collide with the moving platform that the character changes to kinematic mode and the parent is the collider that way the character moves with the collider. so something like this:

void OnCollisionEnter2D (Collision2D coll) {

     if (coll.transform.tag == "Moving Platform") 
     {
         GetComponent<Rigidbody2D>().isKinematic=true;
         transform.parent = coll.transform;
     }
 }

Now that's all well and good but now while it's kinematic and parented it's unable to move on it's own and programming extra controls to control it in this state is a pain so what you want is that if you move or jump then the character is no longer kinematic or parented. This is best done in the functions update and fixedupdate because it will be checked every frame. So something like this:

     if (move != 0 && transform.parent != null) 
     {
         GetComponent<Rigidbody2D>().isKinematic= false;
         transform.parent = null;
     }

This command basically says if your moving and your parented then the player is no longer kinematic and is no longer parented. Then add in your command for jumping so something similar to this:

             if ((grounded) && Input.GetKeyDown (KeyCode.Space)) 
     {
         GetComponent<Rigidbody2D>().isKinematic = false;
         transform.parent = null;
             }

This way when you are grounded (on the moving platform) and when you press your jump key or function then the object is no longer kinematic and is no longer parented.

This way while you are in collision with the moving platform you will be kinematic and moving with the platform because it's your object's parent. But if you are moving or jumping then you are not moving with the platform and the platform is no longer your parent.

Comment
Add comment · Show 2 · 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 patrickcporto · May 09, 2017 at 11:58 PM 0
Share

Thanks a lot, man. It works better if you use OnCollisionStay, though.

avatar image lod_ashtaroth · Dec 06, 2017 at 06:28 PM 0
Share

This is working for me but stretching my character. Any Idea's why?

avatar image
0

Answer by Sebas · Feb 24, 2010 at 10:31 PM

There are already multiple answers for this question on the site. Please use the search option for next time. These Answers should get you started. If questions remain after reading those, feel free to post them.

Answer1

Answer2

Answer3

Comment
Add comment · Show 7 · 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 CracksisT · Feb 24, 2010 at 10:38 PM 0
Share

I already checked those answers and I couldn't get it working with my character. That's why I asked the question myself, providing my own information, hoping that would help.

avatar image Sebas · Feb 24, 2010 at 11:08 PM 0
Share

Not sure which option would be best, but I'd try to implement those other solutions first and ask questions in those answers via comments. What exactly didn't work when you tried those solutions? There are multiple answers provided, so it's not very likely that somebody is going to take the time and come up with something totally new here. Was the joint a problem? Try to break your platform/project down so you can get it to work with one of the provided solutions and so that you understand how they got it working, then expand to suit your needs. But that's just a thought.

avatar image CracksisT · Apr 03, 2010 at 06:35 PM 0
Share

When I use 'Answer1' I get an error saying: Assets/CharStickPlatform.js(26,44): BCE0020: An instance of type 'FPSWalker' is required to access non static member 'speed'. How do I solve this?

avatar image Sebas · Apr 04, 2010 at 12:02 AM 0
Share

Do you use the Character Controller with the FPSWalker script? The script for Answer1 requires you to use the FPSWalker script, because it refers to its variable speed.

avatar image CracksisT · Apr 17, 2010 at 05:17 PM 0
Share

No I probably didn't cause I don't know how to do this. (But I'm not trying to make a FPS, I'm trying to make a 3rd person game.)

Show more comments
avatar image
-1

Answer by Fatback · Jun 22, 2017 at 03:59 PM

Here is a video that allows you too get multiple objects to stick to a platform while allowing you to interact and manipulate them all you want. https://youtu.be/owX_t5OYhFg

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 DJ_RIVA · Feb 04, 2019 at 02:13 PM 0
Share

Unfortunatly the video is down

  • 1
  • 2
  • ›

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

23 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

Related Questions

Player flies upward off the map when it collides with a vertical mesh collider 1 Answer

CharacterController:disable character-to-character collision 2 Answers

CharacterController on Moving Surface 4 Answers

How to prevent Character Controller from falling through moving Platforms? 4 Answers

Trying to avoid collision with sides and interiors of polygons 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