i’ve got an object scripted to follow the mouse via this script :
private var mousePosition:Vector3;
public var moveSpeed = 1.0f;
function Update(){
mousePosition = Input.mousePosition;
mousePosition.z =10;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
transform.position.z = 0;
}
NOTE: the camera z position is -10 in the scene and the object being moved is the child of a canvas but its distance is only 7 away (or -3 actual z world coordinate
it follows the mouse perfectly HOWEVER: the Zed position of the object is always set exactly to 180.962 no matter what i do and i have no clue why. i even tried to manually set the z position after the lerp but no dice. it just refuses to budge. i did some debugging and nothing i printed out said anything about 180 . ever Z value was 0 after all was said and done
i wouldn’t have as big a problem with this if it didn’t shrink the sprite considerably.
any idea why this could be happening? its super annoying!
You got that part correctly, though it might be a good idea to create a new variable to store your ScreenToWorldPoint data in instead of writing over mousePosition, you’ll want to keep that data for clearer debugging.
Secondly, you probably don’t want to Vector2 Lerp a transform.position which is a Vector3. Finally, you might want to try adjusting your approach to moveSpeed. Lerp interpolates between 0 and 1 values. moveSpeed according to the above script is always 1 so there’s no smoothing. A simple solution would be to start at 0 and add Time.deltaTime every frame, so it goes to 1 in 1 second. However, you probably don’t want smoothing to be over in 1 second.
So I’d recommend instead using Vector3.MoveTowards which can take Time.deltaTime as the “speed” and will always interpolate at a constant speed if the start and positions change.
Hey thanks for the response i’ll definitely try seperate variables
regarding:
Hey thanks for the response i’ll definitely try seperate variables
i forgot to update the code: i took this snippit from another answer and modified it a tad. the smoothing is something i didn’t want in the first place lol so its all good on that front. now its just a "transform.position = mousePosition
Update: uppon using different variables i found that in both instances the z position never changed
as you can see the rect transform zed variable is for some reason 180. something i’m still not totaly understanding
i’ll fiddle with the hierarchy and see if it has anything to do with the fact that it’s a child object
EDIT: so uppon further inspection i found that it has SOMETHING to do with the fact that the canvas is scaled down to fit inside the camera’s view range. i have no idea why this makes any difference. and i can’t really do things any other way. very odd.
Have you Debug.Log’d the transform.position.z? And have you checked other scripts that might affect the position? For example anything in LateUpdate() could be causing the problem, or a script with a different execution order. You might want to go back to the basics and try the script on a simpler set up maybe?
as things are its literally about as simple as it gets. the only script on the object is that one (and the script for drawing the image) and yeah several debug.logs later it still shows as perfectly normal. nothing else is modifying that or any other objects in the scene and no scripts in the project have lateupdate.
so it turns out its due to the fact that the canvas is scaled to fit infront of the camera (for editing) and that’s whats throwing off the calculations…really not sure how im supposed to fix that…
EDIT: solved it!!! so basically i used your “different variables” idea and made a new variable that had the screentoworldspace x and y but not the Z therefore nothing in the game changes it and thus problem solved
You can’t set transform.position’s values individually (surprised you’re not getting a warning), so transform.position.z = 10 does nothing, which is why it’s taking the mousePosition’s z value.