0

I am trying to make a 2D "Ghost Object" in Unity that moves to where the player's mouse position is (while snapping to a grid) and rotates when the mouse wheel is rotated. When the player clicks, it would then create the "Real Object" in its place. The problem is that as I start rotating the object, it becomes inexplicably unstable and eventually flies off screen before reaching infinity. When not rotated, it behaves perfectly.

Since the movement code should work regardless of direction, I have no idea why it is flying off screen.

Relevent code (C#):

Vector3 rawPos = cam.ScreenToWorldPoint (Input.mousePosition);
transform.Rotate(0,0,50*Input.GetAxis ("Mouse ScrollWheel"));
Vector3 targetPos = new Vector3 (Mathf.RoundToInt(rawPos.x),Mathf.RoundToInt(rawPos.y),10);
transform.Translate (targetPos-transform.position);

This is probably something obvious, but I'm still a bit new to this. Thanks in advance for any help.

Edit: I suppose I should note that this is occurring in FixedUpdate.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165

1 Answers1

0

Solved the problem by changing

transform.Translate 

to

transform.position = targetPos

Works just as well.