- Home /
Strafe radius increases gradually while top-down sprite is strafing around mouse pointer
I'm having an issue that I can't seem to solve wherein strafing around the mouse pointer seems to gradually cause the radius of the strafing circle to expand resulting in the sprite gradually moving further and further away from the mouse.
Basically, when strafing without any forward or backward motion, I would like the path to form a relatively perfect circle, but my code seems to be causing it to follow a gradually expanding spiral path. I'm completely stumped.
Here's the code:
public Vector2 speed = new Vector2 (5, 5);
public float maxSpeed = 5f;
private Vector2 movement;
private Vector2 movementX;
// Update is called once per frame
void Update () {
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
float inputX = Input.GetAxis ("Horizontal");
float inputY = Input.GetAxis ("Vertical");
Vector3 diff = mousePos - transform.position;
if (diff.x < 0.1 && diff.x > -0.1 &&
diff.y < 0.1 && diff.y > -0.1) {
if (inputY > 0) {
inputY = 0f ;
}
inputX = 0f;
}
movement = transform.up * maxSpeed * inputY;
movementX = new Vector2 (transform.up.y, -transform.up.x);
movementX *= maxSpeed * inputX;
}
void FixedUpdate() {
rigidbody2D.velocity = movement + movementX;
}
Any suggestions would be greatly appreciated! Thanks for your time.
Comment
Your answer
