- Home /
2d Spaceship smooth mouse movement
I'm using a mosue to move my player on a 2d spaceship game.
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Mouse X") * speedx;
float moveVertical = Input.GetAxis ("Mouse Y") * speedy;
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody> ().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
I've added a float (speedx and speedy) to speed up the movement because it wasn't following my mouse enough quickly. When it comes on the boundary, the ship is kind of lagy a lil. I'm new in coding so I would realy apreciate any help to fix the smoothness of the ship!
Your code seems to be fine apart the fact that you've added speedx and speedy even though you already have a "speed" multiplier in you line 7, so why just not increase that one ins$$anonymous$$d or remove it if your mouse vertical and horizontal speed differs?
Upload a video somewhere that shows your issue. But in any case the code above is fine so far that you provide all values it require.
P.S. And FYI you're not in 2D but in 3D even though you move only in 2 directions.