- Home /
Getting the player to move with platform: Velocity, DistanceJoint, or...?
Hello, my player is parented to the platform OK if I move its position, but if I want a platform to move in a constant direction by applying velocity, the player just slides off. I've read what I could in other answers, but haven't found exactly what I need. Here's the code I'm trying to use:
using UnityEngine;
using System.Collections;
public class MoveWithScript : MonoBehaviour {
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.tag == "Player") {
other.transform.parent = gameObject.transform;
other.gameObject.rigidbody2D.velocity = gameObject.rigidbody2D.velocity;
}
}
void OnCollisionExit2D (Collision2D other){
if (other.gameObject.tag == "Player") {
other.transform.parent = null;
other.gameObject.rigidbody2D.velocity = other.gameObject.rigidbody2D.velocity;
}
}
}
Thanks for reading. Your help is greatly appreciated :)
Answer by HarshadK · Oct 09, 2014 at 12:00 PM
Do not parent a rigidbody to another rigidbody. You will get quirky results.
Rather try joint to make your player stick to the platform.
Thanks for the advice Harshad$$anonymous$$. I've been seeing some of those quirky results as I've been trying different things! I'll look into using joint. If you know of any good examples or tutorials in an example like this, please point me in their direction :)
You can create a joint at runtime wherein you add the DistanceJoint2D component to your platform and set player as the connectedBody. And to break the joint just apply the force greater than the break force of that joint.
Thanks Harshad$$anonymous$$. I'm still pretty new to all this, so I'm doing my homework on how to do what you stated above :D I tried to start with this, but it's also giving me pretty quirky results:
void OnCollisionStay2D (Collision2D collision){
if (collision.gameObject.tag == "Player") {
var disJoint = gameObject.AddComponent<DistanceJoint2D>();
disJoint.connectedBody = collision.gameObject.rigidbody2D;
}
}
}
Try to use the Anchor and Connected Anchor property of your joint so the two bodies are anchored properly.
Since this will allow you to join your two bodies specific to their current position with respect to each other.
Answer by Kiwasi · Oct 10, 2014 at 12:20 AM
You could make your code work as is just by changing to OnCollisionStay. Ultimately this will lock your players movement to match the platforms movement, which is typically undesirable.
A better method would be to compare the velocity of the player and the platform, and add force to the player proportional to the difference in velocities.
You could get even more realistic performance by getting your players mass right (A human typically weighs between 60-80 kg). With the right physics material this should provide enough force from gravity to hold the player to your platform at low accelerations. No scripts required.
Thanks Bored$$anonymous$$ormon. I'll look into using those options! I did trying comparing the velocities of the player and platform, then equating them. No luck there, though I'm missing why it doesn't work:
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.tag == "Player") {
if (other.gameObject.rigidbody2D.velocity != this.gameObject.rigidbody2D.velocity) {
other.gameObject.rigidbody2D.velocity = this.gameObject.rigidbody2D.velocity;
You missed the first line of my answer.
OnCollisionEnter is only called once. So the players velocity matches the platform for one frame. Then physics happens and the player slides off.
OnCollisionStay will be called every frame the player and platform are colliding. Thus physics will be overridden.
You totally missed the point about comparing velocities. Try this
void OnCollisionStay2D (Collision2D collision){
if (collision.gameObject.tag == "Player") {
collision.rigidbody2D.AddForce(1000* (rigidbody2D.velocity - collision.rigidbody2D.velocity));
}
}
Sorry I misread your answer. Thanks for the reply. I was connecting the undesirable part to using OnCollisionStay2D. I tried the code you provided (and tried adjusting it), but something still isn't working. The player jitters and is forced left or right off the platform.
Does your platform need to be a rigidbody? If you can move the platform in the same way without having a rigidbody attached to it then parenting your player to it while in contact with it should work perfectly.
Thanks $$anonymous$$rSoad. I'm actually trying to use that method now and move the transform rather than move the rigidbody with velocity :)