1
\$\begingroup\$

I'm having a problem similar to this link: http://answers.unity3d.com/questions/1295955/character-on-a-moving-platform-slows-down.html

But...

My 2D character is on an elevator (which does not have any physics) and is being moved up and down through an animation (transform). But when my character is on the elevator, the colliders2D overlaps another, as my character's RigidBody2D stands still as I move the game object elevator (crossing each other). My solution was to try to attach my character as the child of the elevator (SetParent), and this solved the problem of overcoming the colliders, but it gave me a new problem.

The new problem consists of a certain slowness in the movement of the character while it is on the elevator. I believe the problem is because the transform of my character is in motion while the elevator goes up and my rigidbody don't add any force.

Do I need move the elevator with physics to stay movent? Or am I thinking the wrong way?" How to solve this problem? See the player movement script:

 using UnityEngine;
 using System.Collections;

 public class Move : MonoBehaviour {

     public float speed;
     public float jump;
     public GameObject rayOrigin;
     public float rayCheckDistance;
     Rigidbody2D rb;

     void Start () {
         rb = GetComponent <Rigidbody2D> ();
     }

     void FixedUpdate () {
         float x = Input.GetAxis ("Horizontal");
         if (Input.GetAxis ("Jump") > 0) {
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin.transform.position, Vector2.down, rayCheckDistance);
             if (hit.collider != null) {
                 rb.AddForce (Vector2.up * jump, ForceMode2D.Impulse);
             }
         }
         rb.velocity = new Vector3 (x * speed, rb.velocity.y, 0);

     }
 }
\$\endgroup\$
1
\$\begingroup\$

Instead of adding the character as a child of the lift it would be better to get the lift to push the player up with rigidbody and add force. The animation of the lift will update on update but all other physics will work on the fixedupdate.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your response. But in that case, are you telling to add a RigidBody on elevator? I have problems with overlap of colliders when I try to do this. Or I need to add force to both rigid bodies? Today I add only on the body of the elevator. Is that the problem? \$\endgroup\$ – Igor Oct 25 '17 at 12:22
  • 1
    \$\begingroup\$ @Igao you would need a rigidbody on the elevator yes, but it should work with only velocity or force added to the elevator, I have checked and it works fine for me. \$\endgroup\$ – PStag Oct 25 '17 at 19:31
  • \$\begingroup\$ your movement horizontally not affected? Do you have a sample please? \$\endgroup\$ – Igor Oct 27 '17 at 2:16

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.