0
\$\begingroup\$

My character is not sticking on the platform when jumping on it, but I don't know why. I checked, and I have rigidbodys and colliders. The problem appeared when I moved the game to another hard drive.

Here is mycode:

void OnCollisionEnter2D(Collision2D other)
{
    if (other.transform.tag == "MovingPlatform") 
    {
        transform.parent = other.transform;
    }
}

void OnCollisionExit2D(Collision2D other)
{
    if (other.transform.tag == "MovingPlatform") 
    {
        transform.parent = null;
    }
}

Why doesn't my character stick to the platform?

\$\endgroup\$
1
  • \$\begingroup\$ Please make sure you have Rigidbody2D and not Rigidbody on both objects, as @Alakanu have pointed out. \$\endgroup\$ Jan 10, 2017 at 6:03

3 Answers 3

1
\$\begingroup\$

I'm going to assume that both objects have a ridgidbody attached to them.

void OnCollisionStay2D(Collision2D other)
{
    if (other.transform.tag == "MovingPlatform") 
    {
        GetComponent<RigidBody>().velocity 
                = other.gameObject.GetComponent<RigidBody>().velocity;
    }  
}   

This should make the character stick to the platform. You might want to save each ridgidbody in the OnCollisionEnter method and then call them in the Update method, to get a bit more efficiency out of it, so that you can also move off the platform by adding velocity along the Y-axis.

\$\endgroup\$
0
0
\$\begingroup\$

The problem appeared when I moved the game to another hard drive.

Well there is your problem right there. I have noticed that unity tend to loose the custom tag definitions when moving the game. Especially if you use packages for that. Check if your tags are defined.

\$\endgroup\$
-2
\$\begingroup\$

From the poor information you shared, the only thing that comes up to my mind is that you are using a Rigidbody instead of a Rigidbody2D.

\$\endgroup\$
3
  • \$\begingroup\$ This does not seem like a very good answer. A simple comment could have clarified this, instead of making a guess. \$\endgroup\$
    – Gnemlock
    Dec 2, 2016 at 0:07
  • \$\begingroup\$ This does not seem like a very good comment. A simple downvote could have showed your point, instead of reproving me with a comment. \$\endgroup\$
    – Alakanu
    Dec 9, 2016 at 14:05
  • 2
    \$\begingroup\$ I'm sorry if you took offense. For future, generally, we recommend you comment to provide reasoning for downvotes (you will likely be told so again, when you earn that privilege), so the user may improve on their question or answer. \$\endgroup\$
    – Gnemlock
    Dec 9, 2016 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .