- Home /
2D Movement [HELP!]
How do I make it so that the player moves towards the mouse with velocity?
if(Input.GetKey (KeyCode.W)){
facing = 3;
transform.position = Vector3.MoveTowards(transform.position, pos, movementspeedCurrent * Time.deltaTime);
}
This code doesn't use velocity, but does make the character move towards the mouse. Only problem is that it moves INSIDE of the objects it collides with, it teleports in a little bit and teleports out, and repeats. Colliding doesn't work.
rigidbody2D.velocity = new Vector2(movementspeedCurrent,0);
this works, no collisions issues, it hits the object it collides with, and instantly stops. This is what I want. Only problem is that it doesn't move to the mouse, and I'd expect that, because I'm only making it move left/right with this code.
This is why I like velocity, the collisions are precise. Although, if there is a way to fix the player moving in and out of the object it collides with, and making it just stop solidly when colliding with an object with a Box Collider 2D, please help.
I don't HAVE to use Velocity, that's all I know that works at this time. If you've got a better way to move the character, please, tell me.
rigidbody2D.velocity = Vector2.MoveTowards(transform.position, targetPos, movementspeedCurrent*Time.deltaTime);
tried this and expected it not to work. It does not work.
This game is 2D with an Orthographic camera.
Edit: Overhead game