Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Keep the character the same apparent size by moving plane or camera

Discussion in 'Editor & General Support' started by valter-home, Sep 20, 2022.

  1. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    In my editor with camera set in perspective projection I move 2D and 3D objects on a new Plane with the mouse while working in Game View. Objects are instantiated through an ObjectField by dragging them from Project folders. As for the Sprites, I need that they always remain the same apparent size as they are moved away from or brought closer to the camera and even if I lower or raise the plane on which they are positioned. With
    Code (CSharp):
    1. mySprite.transform.localScale = InitialSizeV3 * (Mathf.Abs((new Plane(cam.transform.forward, cam.transform.position).GetDistanceToPoint(mySprite.transform.position)) / Mathf.Abs(cam.transform.position.z)));
    I get what is requested. Furthermore, I also maintain their position on the screen as I move the Plane or Camera using
    Code (CSharp):
    1. previousPos = cam.WorldToScreenPoint(mySprite.transform.position);
    and then, after moving the Plane or the Camera
    Code (CSharp):
    1. Ray ray = cam.ScreenPointToRay(previousPos);
    2. if (myPlane.Raycast(ray, out enter)) {
    3. mySprite.transform.position = ray.GetPoint(enter);
    4. }
    The problem arises with the 3D object, which in my case is a character. When I move the character away or closer to the camera I don't want it to keep the same apparent size as for the Sprites but rather to resize itself normally. So I don't use the above code.
    But when I move the Plane or the Camera the character obviously changes proportion on the screen according to his position with respect to the camera and that's where I can't find a solution.
    If I move the character on the z axis, it will resize normally (as it does) but if I move the camera or raise / lower the Plane I would like it to keep the same proportions on the screen. To complicate things, I also need to be able to resize it anywhere on the screen but always, by moving the Plane or the Camera, it should keep the same apparent size as the last assigned scale and where on the screen it is.
    The above code for sprites only works if I don't move the character from the position in which it is instantiated. If I bring the character closer to the camera obviously it appears larger (further away, smaller) and this is the apparent size that I should keep. Instead, this code naturally resizes my character to the same size wherever it is on the screen.
    Ultimately, the character has to change its size as I drag it with the mouse on the z axis, but it should remain the same apparent size when I move the Plane or the Camera respecting the last assigned scale and where on the screen it is.
    I've been looking for a solution for several days and need some advice. I just can't find the correct algorithm.
     
    Last edited: Sep 21, 2022
  2. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    73
    I found the answer.
    The character is free to resize by moving it with the mouse on the Z axis but with the following lines it maintains its proportion both while I move the Plane or the Camera on the Y axis (Camera Z is fixed). Even changing its scale at any time.

    Code (CSharp):
    1. Vector3 scale = ( (PlaneY - CameraY) * InitialScaleCharacter ) / InitialPlaneY - InitialCamY;
    2. char.transform.localScale = scale;
     
    Last edited: Sep 21, 2022