Christian Dieckmann
Christian Dieckmann

Reputation: 1

Object won't move even with velocity -Unity-

I got the problem that my object won't move when I apply a velocity in.

Rigidbody2D Rigidb;
float MovingSpeed = 5f;

void Awake()
{
    Rigidb = GetComponent<Rigidbody2D>();
}

void MoveObject(Vector3 destination)
{
    Vector3 dirVector = destination - transform.position;

    bool x = false, y = false;

    if (dirVector.x < 0)
    {
        x = true;
        dirVector.x = -dirVector.x;
     }
     if (dirVector.y < 0)
     {
         y = true;
         dirVector.y = -dirVector.y;
     }

     if (dirVector.x > dirVector.y)
     {
         dirVector = dirVector / dirVector.x;
     }
     else
     {
         dirVector = dirVector / dirVector.y;
     }

     if ((x && dirVector.x > 0) || (!x && dirVector.x < 0)) dirVector.x = -dirVector.x;
     if ((y && dirVector.y > 0) || (!y && dirVector.y < 0)) dirVector.y = -dirVector.y;    
     Rigidb.velocity = new Vector2(dirVector.x * MovingSpeed, dirVector.y * MovingSpeed);
}

In my testcases the velocity ends up being (0.0, 5.0) but even though it just won't move. It isn't kinematic and the x and y movement isn't frozen either.

Upvotes: 0

Views: 2759

Answers (1)

Endrik
Endrik

Reputation: 606

Make sure you have your rigidbody isKinematic to false, and position constraint to not freeze to x,y,z axis

Upvotes: 1

Related Questions