MovePosition/Velocity not following a direction
Hello !
I have a quick question about Unity physics and lerping to a direction point. My rigidbody is set to non-kinematic, gravity = 0, same for angles = 0, and when I debug the magnitude of my direction is at 12 when I launch the item forward (x = 0, y = 12) and more than 12 when it's a diagonal move.
I tried everything, velocity, moveposition, clamping magnitude to 1 or 12 (in my case), normalize the direction, nothing is working the way I want. The item goes in a different direction and not the one I affect to velocity or moveposition.
//My unique direction
_direction = new Vector2(_mousePosX, _mousePosY).normalized;
//Want the item to follow exactly this direction line
_item.GetComponent<Rigidbody2D>().velocity = _direction;
This is a quick pic of my issue :
I don't get why my object doesn't go follow the direction, it's like it is controlled by something else but I can't figure what.. I just want my velocity to be equals as a normal lerp like from the origin point to the direction point. But velocity seems to not act like a normal lerp, it has something that modify the final direction point to an other controlled by physic?
I have no issues with colliders, no issue to lerp the object with physics but the only issue is that the item does not exactly follow the direction line.
I searched for hours, reading all topics, can't figure what is wrong with my code. If you can help me, it'll be much appreciated. Thank you in advance.
what are you moving to? velocity towards mouse position? or add velocity when mouse moves? the rest of the script would be helpful
$$anonymous$$oving a scene object towards the mouse position mixes two different coordinate system. And even though it looks like the mouse moves over the exact spot you see in the scene it's still pixels vs. units. Try transfor$$anonymous$$g the mouse coordinate correctly into the scene.
//$$anonymous$$y unique direction
_direction = Camera.main.ScreenToWorldPoint(new Vector3(_mousePosX, _mousePosY,0)).normalized;
//Want the item to follow exactly this direction line
_item.GetComponent<Rigidbody2D>().velocity = _direction;
This method assumes your camera is orthographic. Otherwise the z value of the transformed Vector3 must be larger than 0.