- Home /
 
 
               Question by 
               DiamondMC102 · Aug 29, 2017 at 08:29 PM · 
                2d-platformerrigidbody2dvelocity2d-physics  
              
 
              How to check if Rigidbody2d is moving
How to check if Rigidbody2d is moving .
Here my code if it helps you.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Indingo_Movement : MonoBehaviour
 {
 
 public int runMod; // Run Speed multiplier
 
 private Rigidbody2D MyRigidBody;
 
 private Animator MyAnimator;
 
 [SerializeField]
 private float movementSpeed;
 
 private bool facingRight;
 
 private PolygonCollider2D myPolygonCollider2D;
 
 // Use this for initialization
 void Start()
 {
 facingRight = true;
 MyRigidBody = GetComponent<Rigidbody2D>();
 MyAnimator = GetComponent<Animator>();
 myPolygonCollider2D = GetComponent<PolygonCollider2D>();
 }
 
 // Update is called once per frame
 void FixedUpdate()
 {
 
 float horizontal = Input.GetAxis(“Horizontal”);
 
 HandleMovement(horizontal);
 
 Flip(horizontal);
 
 Deactivator();
 
 }
 
 private void HandleMovement(float horizontal)
 {
 if (Input.GetKey(KeyCode.RightShift))
 {
 MyRigidBody.velocity = new Vector2(horizontal * movementSpeed * runMod, MyRigidBody.velocity.y); // x – 1, y = 0;
 
 MyAnimator.SetFloat(“speed”, Mathf.Abs(horizontal));
 MyAnimator.SetBool(“isRun”, true);
 
 }
 else
 {
 
 MyRigidBody.velocity = new Vector2(horizontal * movementSpeed, MyRigidBody.velocity.y); // x – 1, y = 0;
 MyAnimator.SetBool(“isRun”, false);
 
 MyAnimator.SetFloat(“speed”, Mathf.Abs(horizontal));
 }
 }
 
 private void Flip(float horizontal)
 {
 if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
 {
 facingRight = !facingRight;
 
 Vector3 thescale = transform.localScale;
 
 thescale.x *= -1;
 
 transform.localScale = thescale;
 }
 }
 
 private void Deactivator ()
 {
 if (Input.GetAxis(“Horizontal”) > 0)
 {
 
 myPolygonCollider2D.enabled = false;
 
 }
 }
 }
 
              
               Comment
              
 
               
              Answer by LuckyFeathers · Aug 29, 2017 at 08:35 PM
Declare a Vector2 lastPosition and set it to your position in void Start(). Then run this in update:
 if (lastPosition != transform.position)
         {
             //code
         }
 
              Your answer
 
             Follow this Question
Related Questions
Why doesn't Rigidbody.velocity.x = 0 in a collision? 1 Answer
2D Character won't jump diagonally 1 Answer
Clipping bug I have encountered... 0 Answers
Rigidbody2D.velocity.x not working but Y does 0 Answers
HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers