0
\$\begingroup\$

I have a few basic platforming mechanics going on in this scene. I have a prefab platform with an animation that loops back and forth between two different x positions, but does not begin looping until OnCollisionStay2D activates with my player object with the tag "Red cube".

    using UnityEngine;
     using System.Collections;


public class PlatformMovement : MonoBehaviour {

        GameObject player;

        void Start()
        {
            player = GameObject.FindGameObjectWithTag("Red Cube");
        }

        void OnCollisionStay2D(Collision2D coll) 
        {
            if (coll.gameObject.tag == "Red Cube") 
            {
                Debug.Log ("Something has entered this zone.");
                GetComponent<Animation> ().Play ("Side To Side");
                player.transform.SetParent(transform,true);
                player.GetComponent<Rigidbody2D>().isKinematic = true;
            }
        }
    } 

I would like my Red cube Player object to not slide off of the platform when it begins to move. This is solved through OnCollisionStay2D as well; however, the Red cube Player remains kinematic even after I have left the platform. I tried to solve this with

void OnCollisionExit2D(Collision2D coll)
        {
            if(coll.gameObject.tag == "Red Cube")
            {
                Debug.Log (" Honkey has left the platform.");
                player.transform.SetParent(null);
                player.GetComponent<Rigidbody2D>().isKinematic = false;

            }

But for some reason the Red Player cube still falls of... Is there an error here that I am missing? Thank you for your time!

\$\endgroup\$
  • \$\begingroup\$ It may be helpful to see what your player controller looks like too, as this could be an interaction between the way you're moving the platform, and the way the player object is moved by player input. \$\endgroup\$ – DMGregory Aug 7 '15 at 14:00
1
\$\begingroup\$

You should try using a Platform Effector.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

You could increase the friction when player enters the collider of the platform, you can increase friction like this: GetComponent '<'Collider2D'>' (). sharedMaterial.friction = VALUE FROM 0 TO 1 (FLOAT)

without ''

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

I have not used Unity in about a year now so I don't remember a lot of the specifics, but I was just skimming this article earlier today and it looks helpful. (scroll down to moving platforms)

Note that the content below was copied from the link above, and the wording slightly tweaked with Unity in mind.

Moving Platforms

Moving platforms can seem a little tricky, but are actually fairly simple. Unlike normal platforms, they cannot be represented by fixed tiles (for obvious reasons), and instead should be represented by an AABB, that is, a rectangle that cannot be rotated. It is a normal obstacle for all collision purposes, but if you stop here, you’ll have very slippery moving platforms (that is, they work as intended, except that the character does not move along with them).

There are a few different ways to implement that. One algorithm is as follows:

  • Before anything on the scene is stepped, determine whether the character is standing on a moving platform. This can be done by checking, for example, whether his center-bottom pixel is just one pixel above the surface of the platform. If it is, store a handle to the platform and its current position inside the character.
  • Step all moving platforms.
  • After stepping all moving platforms, for every character that’s standing on a moving platform, figure the delta-position of the platform, that is, how much it has moved along each axis. Now, shift the character by the same amount.
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.