Character on moving platform with Interpolation
I am making my first game in Unity and I really enjoy it, but I have run into a very annoying problem. My game is a platform game where the character is always running right. My character has a rigidbody2D component attached to it. I am now trying to implement platforms moving up and down and I am using this script:
void Update()
{
transform.position = Vector2.MoveTowards (transform.position, patrolPoints [pointToReach].position,moveSpeed * Time.deltaTime);
if (transform.position == patrolPoints [pointToReach].position)
{
if (pointToReach == patrolPoints.Length - 1)
{
pointToReach = 0;
}
else
{
pointToReach++;
}
}
}
I move the character by changing the velocity of the Rigidbody2D component. That is done with the code:
void FixedUpdate ()
{
if (alive && !levelWon)
{
if (playerBody.velocity.x <= 0 && !onGround)
{
playerBody.velocity = new Vector2 (playerBody.velocity.x, playerBody.velocity.y);
}
else
{
playerBody.velocity = new Vector2 (moveSpeed, playerBody.velocity.y);
}
}
}
It works beautifully but my problem is when my character lands on the moving platform, he is not moving smoothly with the platform because he is not attached to it. I came across a solution that I really like that involves making the character a child of the platform as long as it is on it so I used that:
void OnCollisionEnter2D(Collision2D target)
{
if (target.gameObject.tag == "MovingPlatform")
{
transform.parent = target.gameObject.transform;
}
}
void OnCollisionExit2D(Collision2D target)
{
if (target.gameObject.tag == "MovingPlatform")
{
transform.parent = null;
}
}
It works when I use the Interpolate setting None but the movement of my character is not smooth with this setting. I have been using interpolation with very smooth movement but it does not work with the moving platform. As soon as it lands on the platform and becomes a child of the platform it moves much more slowly than before, with extrapolation the problem is the opposite, he speeds up.
I hope I have made my problem clear, any thoughts or help is much appreciated
Thanks in advance - The confused game developer
Could you perhaps post your movement code? There could be many issues with this: 1. gravity - though should not be a problem. But maybe you can have some axis constraints that will solve it. 2. user going in and out its parent - due to its movement, it could potentially cause some jittering. 3. faulty movement code relative to parent
if you have more information, please provide.
I have updated the post so it now shows how I move the character too. Thank you for taking the time to help me out.
Well, Unity developers say not to directly change the velocity. Read it here: http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
you have 2 choices as I see it:
Use
Rigidbody.AddForce(direction * speed * Time.deltaTime)
: http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.htmlOr better for 2d, use translate on the transform, directly moving it a fixed distance with every frame
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
: http://docs.unity3d.com/ScriptReference/Transform.Translate.html
Try to change it and see maybe it'll solve your issues.
Share an exported project if you wish and we can take a look. Im at myUserName+at+gmail
If you're moving with Update(), this will happen. Update() cycles are called as fast as your machine will allow every Update to finish. They are not in sync with physics updates that occur in sync with FixedUpdate(). As a result, you'll get jitter when colliders are near each other.
As for the platforms, have you looked into physics material and friction / drag?
The platforms are moved up and down within Update() between two points. $$anonymous$$y character is controlled by setting the horizontal velocity to a variable moveSpeed within FixedUpdate(). I have posted the code for the characters movement
Answer by HarpDernier · Jul 03, 2018 at 06:10 PM
Hey I had this exact same problem. Hopefully this won't be my final solution but it seems to work in the mean time. In the script that parents the player to the moving platform I set the interpolation method to none when they connect and then set the interpolation method back to interpolate when they disconnect.
public class StickToPlatform : MonoBehaviour
{
Rigidbody2D BotBody;
void Start ()
{
BotBody = GetComponent<Rigidbody2D> ();
}
void OnTriggerEnter2D (Collider2D info)
{
if (info.tag == "Moving Platform")
{
transform.parent = info.transform;
BotBody.interpolation = RigidbodyInterpolation2D.None;
}
}
void OnTriggerExit2D (Collider2D info)
{
if (info.tag == "Moving Platform" && transform.parent == info.transform)
{
transform.parent = null;
BotBody.interpolation = RigidbodyInterpolation2D.Interpolate;
}
}
}
Your answer
Follow this Question
Related Questions
2d Moving Platforms 1 Answer
Character flickering on moving platform 3 Answers
Rigidbody (player) moves slower on moving platform 2 Answers
Freeze character after collision 0 Answers
Moving RigidBody Smoothly 0 Answers