But my object still starts in the previous position of where I last tapped the screen. I have the game level and a lose level. When I tap a button on the lose level screen to restart the game, my object starts in the exact same x position where I had previously tapped the screen to restart the game.
How can I make it so that my object starts where I want it to when the level restarts as opposed to the object positioning itself where I last tapped the screen on the previous level?
I think the more accurate and helpful way to look at it is: the object is immediately moving to where the mouse is on the first frame of the new level. Where it was on the previous level has nothing to do with it. Your code (in Update) says to go where the mouse is, and so that’s what it does.
So, the obvious way to make it not do that is to delete the code in your Update method. But you clearly have that in there for some reason… so it’s up to you to figure out when you want it to go to the mouse position, and when you don’t, and then program that logic in.
Yeah that code is in there to control the object. I just want it to start at a certain location before controlling it.
Even testing on a phone, the moment I touch the restart level button in the previous level, the game level starts the object in the exact same x position as where my finger had clicked on the button in the previous level. Thats why I get confused. I understand it taking the mouse cursor position while testing in Unity, but why is it taking the same position after I have lifted my finger from the restart level button?
The mouse position has to report something. There is no sensible way to report “no value” for a Vector3. So, when there is an actual mouse cursor, Input.mousePosition reports where that is. When there is not, as when running on a touch interface device like a phone, then Input.mousePosition reports the position of the current or last primary touch.
The fact that Unity is faking a mouse for you at all is strictly a convenience. They could just have mousePosition always return Vector3.zero on touch devices, and force you to use Input.touches to dig out the information you need. But in many cases, it’s darned handy to ignore multi-touch, or the possibility of no touches at all, and just treat the user’s finger like a mouse. But you must accept that the position is still there, even after the finger has left.
You can easily tell when this is the case by looking at Input.GetMouseButton(0), since they also fake the mouse button. So, perhaps you only want to move your object when Input.GetMouseButton(0)?