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 junkrar · Mar 26, 2013 at 04:22 AM · c#2d-platformermoving platform

Moving Platforms

I'm writing a 2D Platformer and I'm trying to get the player to stay on a moving platform. I've done searching and tinkering for a day or two now, and I'm not having any luck.

Basically, I've been told to try to keep the character moving with the platform when they are touching. Firstly, if I use anything related to OnTriggerEnter(), the player goes right through the platform. If I do OnCollisionEnter() (with a CharacterController on the player and a BoxCollider on the platform), nothing happens at all. These are two things I've found suggested most. The other is parenting the player with the platform, but this apparently causes "problems" (frequently stated, never explained).

So, how can I get the player to stay on the moving platform? Here is the code for the moving platform:

 public class MovingPlatform : MonoBehaviour
 {
     private float useSpeed;
     public float directionSpeed = 9.0f;
     float origY;
     public float distance = 10.0f;
 
     // Use this for initialization
     void Start () 
     {
         origY = transform.position.y;
         useSpeed = -directionSpeed;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(origY - transform.position.y > distance)
         {
             useSpeed = directionSpeed; //flip direction
         }
         else if(origY - transform.position.y < -distance)
         {
             useSpeed = -directionSpeed; //flip direction
         }
         transform.Translate(0,useSpeed*Time.deltaTime,0);
     }

AND here is the code for the Player's movement (in Update):

 CharacterController controller = GetComponent<CharacterController>();
         float rotation = Input.GetAxis("Horizontal");
         if(controller.isGrounded)
         {
             moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
             moveDirection = transform.TransformDirection(moveDirection);
             
             //running code
             if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
             { running = true; }
             else
             { running = false; }
             
             moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
             
             //jump code
             if(Input.GetButtonDown("Jump"))
             {
                 //moveDirection.y = jumpHeight;
                 jump ();
             }
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
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

Answer by Spy-King · Nov 01, 2015 at 01:27 PM

You can also use the code in this video. It will work for both 2D and 3D.

https://youtu.be/t6Xr6iRMWII

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

Answer by sarmth · Mar 26, 2013 at 05:01 AM

The best way to achieve this is by the use of unity's in built physics system. Using the Rigidbody feature you can achieve this easily.

See here for more information;

http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html

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 junkrar · Mar 26, 2013 at 05:11 AM 0
Share

Hi, I'm trying this right now, but it doesn't seem to be having any affect. I've added a rigidbody to both the player and the platform, and set the platform to be kinematic (but not to use gravity) and for the player to use gravity but not kinematic. The player doesn't always go through the platforms, but when the platform comes out of the ground from below, it doesn't pick the character up, and the player jiggles when I actually get it on the platform.

avatar image sarmth · Mar 26, 2013 at 08:47 AM 0
Share

Have you added a collider (non-trigger) to the player object? Also make sure you have the collision detection set to constant / strict. Also, I think the second object (platform) should not be kinematic.

avatar image junkrar · Mar 26, 2013 at 02:18 PM 0
Share

The reason I set it as $$anonymous$$inematic was because the rigidbody documentation you linked said "If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached." Also, I have no individual collider on the player object because it has a character controller, which replaces the collider. How do I set the collision detection?

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

my 2d character is not going to next animation state from one 0 Answers

Transform.position assign attempt not valid, and position is infinity on an object? 1 Answer

Dynamic parenting to moving platform so object won't slide from it on it own doesn't work properly 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