title: Move an enemy on top of a moving platform
body: <p>I have a setup which involves a moving platform(in green) and a patrolling enemy on top of it, shown below:</p>
  
  <p><a href="https://i.stack.imgur.com/X6KjE.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X6KjE.gif" alt="enter image description here"></a></p>
  
  <p>The platform is moving left and right, periodically and so is the patrolling enemy both using <code>rigidbody.velocity</code>. Both when in motion separately are appearing fine. But when enemy is on the top of the platform, with enemy being the childobject of platform, the enemy is appearing static (just playing moving animation in it's place) even though I have corrected for the velocity of the platform. Below is the snippet of my code for patrolling enemy:</p>
  
  <pre><code>void Start()
      {
          myRigidBody = GetComponent&lt;Rigidbody2D&gt;();
          parent = transform.parent;
          parentVelocity = new Vector2(0f, 0f);
      }
  
      // Update is called once per frame
      void Update()
      {
          if (parent != null &amp;&amp; parent.GetComponent&lt;Rigidbody2D&gt;() != null) 
              parentVelocity = parent.GetComponent&lt;Rigidbody2D&gt;().velocity;
  
          Debug.Log("parentVelocity=== " + parentVelocity);
          if (IsFacingRightOrUp())
          {
              if (moveVertical)
                  myVelocity = new Vector2(0f, moveSpeed);
              else
                  myVelocity = new Vector2(moveSpeed, 0f);
          }
          else
          {
              if (moveVertical)
                  myVelocity = new Vector2(0f, -moveSpeed);
              else
                  myVelocity = new Vector2(-moveSpeed, 0f);            
          }
          Debug.Log("myRigidBody.velocity === " + myVelocity);
          myRigidBody.velocity = myVelocity + parentVelocity;
          Debug.Log("myRigidBody.velocity === " + myRigidBody.velocity);
      }
  </code></pre>
  
  <p>It is producing the Debug logs as follows, <code>parentVelocity</code> being defaulted to (0,0):</p>
  
  <p><a href="https://i.stack.imgur.com/2vKah.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2vKah.png" alt="enter image description here"></a></p>
  
  <p>What should I do to make the motion of enemy same to an observer on the platform as the motion of enemy would appear when both are standing on the static ground?</p>
  
  <p>The correction applied using <code>myRigidBody.velocity = myVelocity + parentVelocity;</code> doesn't seem to matter at all. The enemy is appearing static regardless of correction. And if I change the velocity of moving platform to zero, it is working as expected. Why is the relative velocity correction not working?</p>
  
  <p>Even if I hardcore the velocity to 100, then also the enemy is appearing to be static, although the rigidbody component correctly displays the velocity as 100.</p>