Search Unity

Strange Z value for object following mouse

Discussion in 'Scripting' started by ackley14, Jul 3, 2015.

  1. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    i've got an object scripted to follow the mouse via this script :

    Code (JavaScript):
    1. private var mousePosition:Vector3;
    2. public var moveSpeed = 1.0f;
    3. function Update(){
    4.     mousePosition = Input.mousePosition;
    5.     mousePosition.z =10;
    6.     mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    7.  
    8.     transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    9.     transform.position.z = 0;
    10. }
    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!
     
    Last edited: Jul 3, 2015
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    First off, see this answered question:

    http://answers.unity3d.com/questions/331558/screentoworldpoint-not-working.html

    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.
     
  3. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    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

    heres the problem so far http://i.imgur.com/xKTcSLZ.png

    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.
     
    Last edited: Jul 3, 2015
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    That's interesting...

    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?
     
  5. ackley14

    ackley14

    Joined:
    Oct 31, 2014
    Posts:
    31
    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 :D and thus problem solved
     
    SomeGuy22 likes this.
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can in JavaScript. It's one of the pieces of synantic sugar the language offers.
     
    GroZZleR likes this.