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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Limit mouse drag speed

Discussion in 'Scripting' started by ellenblomw, Jul 29, 2018.

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hello,

    I have a gameobject that I move in a 2D world with OnMouseDrag. The camera moves with the gameobject with boundaries attached.

    I would like to limit the speed of the gameobject though, right now my airplane fly through the world in record time :p.

    I have made a speed var but I don't really know how to apply it, it gets all buggy when I do. This is the code so far:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class planeMovement : MonoBehaviour {
    4.     private Vector3 screenPoint;
    5.     private Vector3 offset;
    6.     public float speed = 5.0f;
    7.  
    8.     void OnMouseDown()
    9.     {
    10.         screenPoint = Camera.main.WorldToScreenPoint(transform.position);
    11.         offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    12.     }
    13.  
    14.     void OnMouseDrag()
    15.     {
    16.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    17.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    18.         transform.position = curPosition;
    19.     }
    20. }
    21.  
    Does anyone have any idea on how to best implement a speed limit?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I guess you already have a collider attached and seem to be moving the object manually (i.e. not using physics). So maybe something a little like this :-
    Code (CSharp):
    1. public class planeMovement : MonoBehaviour
    2. {
    3.     void OnMouseDown()
    4.     {
    5.         m_lastMousePos = Input.mousePosition;
    6.     }
    7.  
    8.     void OnMouseDrag()
    9.     {
    10.         transform.position += (Input.mousePosition - m_lastMousePos) * speed * Time.deltaTime;
    11.         m_lastMousePos = Input.mousePosition;
    12.     }
    13.  
    14. #pragma warning disable 649
    15.     [SerializeField] [Range(0f, 1f)] float speed = 0.25f;
    16. #pragma warning restore 649
    17.  
    18.     Vector3 m_lastMousePos;
    19. }
     
  3. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    It seems to slow it down but creates another problem with the cursors position. I have to drag and let go, drag and let go etc:



    Is there a way to fix this so that the drag slows down the cursor to the same speed?
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Have you tried ramping the
    speed
    variable up to
    1f
    - does that help?
     
  5. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Same problem, as soon as I get to where the camera starts following, then the mouse cursor moves away from the gameobject :/
     
    Last edited: Jul 29, 2018
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I see what you mean. This is tricky because the mouse is limited to the physical screen and I can't find an easy way for Unity to control the mouse at the OS level.

    So below is what I currently have but it is a bit, ahem, glitchy - the skipFrame is clearly not working smoothly. I have to head out now but maybe you can play around to smooth it out. Hopefully you can see what I'm trying to do :-
    Code (CSharp):
    1. public class planeMovement : MonoBehaviour
    2. {
    3.     void OnMouseDown()
    4.     {
    5.         m_lastMousePos = Input.mousePosition;
    6.         ForceMouse();
    7.     }
    8.  
    9.     void OnMouseDrag()
    10.     {
    11.         if(m_skipFrame)
    12.         {
    13.             ReleaseMouse();
    14.         }
    15.         else
    16.         {
    17.             var thisDelta = (Input.mousePosition - m_lastMousePos);
    18.             m_totalMouseDelta += thisDelta;
    19.             transform.position += thisDelta * speed * Time.deltaTime;
    20.             m_lastMousePos = Input.mousePosition;
    21.  
    22.             if (thisDelta.sqrMagnitude > m_sqrMouseConstraint)
    23.                 ForceMouse();
    24.         }
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         if (m_skipFrame)
    30.         {
    31.             ReleaseMouse();
    32.         }
    33.     }
    34.  
    35.     void ForceMouse()
    36.     {
    37.         Cursor.lockState = CursorLockMode.Locked;
    38.         Cursor.lockState = CursorLockMode.None;
    39.         m_skipFrame = true;
    40.     }
    41.  
    42.     void ReleaseMouse()
    43.     {
    44.         m_lastMousePos = Input.mousePosition;
    45.         m_skipFrame = false;
    46.     }
    47.  
    48. #pragma warning disable 649
    49.     [SerializeField] [Range(0f, 1f)] float speed = 0.25f;
    50. #pragma warning restore 649
    51.  
    52.     readonly float m_sqrMouseConstraint = ( Vector3.one * 5 ).sqrMagnitude;
    53.     Vector3 m_lastMousePos;
    54.     Vector3 m_totalMouseDelta;
    55.     bool m_skipFrame;
    56. }
     
  7. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Ok thank you, I'll try it :D.

    BTW: This is what I want to accomplish:



    That plane that is flying is moved by the player putting the finger on the airplane and dragging it around on the screen making it fly around in a 2D world where it meets different minigames and animations. Its the flying around-function I'm after.
     
  8. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Didn't work :/. Any other solutions out there?
     
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Are you sure that is what is happening? It looks to me more like you point at a location and the plane (which appears to be in a constant forward motion) angles itself in that direction. That would be a bit simpler to implement I think (given how elusive the current solution is proving). :)
     
  10. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Yes I'm sure that is what is happening :). I did my own video, might be easier to see. In this game they only have sidescrolling, but in other games they also have with y coordinate involved:



    What is happening in the video is that the object I press down on follows my finger. When I let go the object drops down.
     
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, this works (I parented the camera to my GameObject to make the camera follow the object) :
    Code (CSharp):
    1. public class planeMovement : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         m_camera = Camera.main;
    6.     }
    7.  
    8.     void OnMouseDown()
    9.     {
    10.         m_active = true;
    11.     }
    12.  
    13.     void OnMouseDrag()
    14.     {
    15.         var aimAt = Input.mousePosition;
    16.         aimAt.z = transform.position.z - m_camera.transform.position.z;
    17.         aimAt = m_camera.ScreenToWorldPoint(aimAt);
    18.         m_aimAngle = aimAt - transform.position;
    19.     }
    20.  
    21.     void OnMouseUp()
    22.     {
    23.         m_active = false;
    24.     }
    25.  
    26.     void LateUpdate()
    27.     {
    28.         if(m_active)
    29.         {
    30.             transform.right = Vector3.RotateTowards(
    31.                   transform.right
    32.                 , m_aimAngle
    33.                 , m_maxRadPerSec * Time.deltaTime
    34.                 , 0f);
    35.             transform.Translate(Vector3.right * speed * Time.deltaTime);
    36.             m_camera.transform.rotation = Quaternion.identity;
    37.         }
    38.     }
    39.  
    40.  
    41. #pragma warning disable 649
    42.     [SerializeField] [Range(0.5f, 7f)] float speed = 1f;
    43. #pragma warning restore 649
    44.  
    45.     const float m_maxRadPerSec = ( 45 * Mathf.Deg2Rad );
    46.     Camera m_camera;
    47.     Vector3 m_aimAngle;
    48.     bool m_active;
    49. }
     
  12. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    You are a genius! Thank you very much, a lot better speed. And I love that you added rotation to the plane! Haha

    But I can't get my camera to follow. I moved my airplane as a child to my Main Camera. Is there something more I should do?
     
  13. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The other way round. Make the plane be the parent. Then the camera will follow. The last line of the LateUpdate keeps the camera oriented flat to the world.
     
  14. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Ok I shall read the code thoroughly to learn what every line does. This was really helpful, I appreciate it a lot!

    Do you know if I can clamp the camera somehow, set boundaries for it? So that it doesn't move out from my background?

    When I had the camera follow script on my camera I created privates for yMax, yMin, xMax and xMin and then added:
    transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(target.position.y, yMin, yMax), transform.position.z);

    Could I use this on your code without screwing everything up?
     
  15. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, so if you are running your own camera controller script, then unparent (what is the right word for that?) the camera from your plane. Remove that last line from LateUpdate that sets the camera rotation.

    Then you should be good to do whatever it was you were doing in your camera controller script.

    The only problem may be if the plane moves away from the centre of the screen. Not sure what will happen then as I did not test that. You will have to try that and see. :)
     
  16. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hmm that takes me back to the situation I had from the start with the movement going in something looking like the speed of light :p. I'll try add boundaries to your working script instead. Keep your fingers crossed for me. Respectfully / n00b :D
     
    Doug_B likes this.
  17. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Fingers are crossed - I'm sure you'll do it. Just keep plugging away. Even small steps will get you there eventually. :)
     
    ellenblomw likes this.
  18. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Solved it. So if anyone else wants to set boundaries for camera and make sure that the gameobject doesn't move out of bounds. Here is how I did it by adding to amazing Doug_B:s code.

    Code (CSharp):
    1.     [SerializeField]
    2.     private float xMax;
    3.  
    4.     [SerializeField]
    5.     private float yMin;
    6.  
    7.     [SerializeField]
    8.     private float yMax;
    9.  
    10.     [SerializeField]
    11.     private float xMin;
    12.  
    13.     private Transform target;
    14.     private Transform airplane;
    15.  
    16.     void Start()
    17.     {
    18.         target = GameObject.Find("Main Camera").transform;
    19.         airplane = GameObject.Find("airplane").transform;
    20.         m_camera = Camera.main;
    21.     }
    22.  
    23.     void OnMouseDown()
    24.     {
    25.         m_active = true;
    26.     }
    27.  
    28.     void OnMouseDrag()
    29.     {
    30.         var aimAt = Input.mousePosition;
    31.         aimAt.z = transform.position.z - m_camera.transform.position.z;
    32.         aimAt = m_camera.ScreenToWorldPoint(aimAt);
    33.         m_aimAngle = aimAt - transform.position;
    34.     }
    35.  
    36.     void OnMouseUp()
    37.     {
    38.         m_active = false;
    39.     }
    40.  
    41.     void LateUpdate()
    42.     {
    43.         if (m_active)
    44.         {
    45.             transform.right = Vector3.RotateTowards(
    46.                   transform.right
    47.                 , m_aimAngle
    48.                 , m_maxRadPerSec * Time.deltaTime
    49.                 , 0f);
    50.             transform.Translate(Vector3.right * speed * Time.deltaTime);
    51.             m_camera.transform.position = new Vector3(Mathf.Clamp(target.position.x, xMin, xMax), Mathf.Clamp(target.position.y, yMin, yMax), -25);
    52.             airplane.transform.position = new Vector3(airplane.position.x, Mathf.Clamp(airplane.position.y, yMin, yMax), airplane.position.z);
    53.             m_camera.transform.rotation = Quaternion.identity;
    54.             if (airplane.transform.position.x > 60)
    55.             {
    56.                 airplane.transform.position = new Vector3(0, airplane.position.y, airplane.position.z);
    57.             }
    58.         }
    59.     }
    60.  
    61.  
    62. #pragma warning disable 649
    63.     [SerializeField] [Range(0.5f, 7f)] float speed = 7f;
    64. #pragma warning restore 649
    65.  
    66.     const float m_maxRadPerSec = (45 * Mathf.Deg2Rad);
    67.     Camera m_camera;
    68.     Vector3 m_aimAngle;
    69.     bool m_active;
    70.  
     
  19. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Doug :D! I am adding some stuff to my game and would like to be able to reuse this great script again. How would I go about changing your script to make it applicable to an object I want to move? AKA not moving like a flying airplane, just making it so I can drag it around.

    Eg: Lets say I have a box. I want to be able to move it with the camera following, but not with the angle, just move it around on the screen.

    I first tried removing rotate and that makes it go slow to the right, but not moving in any other direction. Code:
    Code (CSharp):
    1.   void LateUpdate()
    2.     {
    3.         if (m_active)
    4.         {
    5.  
    6.             transform.Translate(Vector3.right * speed * Time.deltaTime);
    7.  
    8.  airplane.transform.position = new Vector3(Mathf.Clamp(airplane.position.x, xMinA, xMaxA), Mathf.Clamp(airplane.position.y, yMinA, yMaxA), airplane.position.z);
    9. ----------etc------------
    I also tried changing to this:
    Code (CSharp):
    1.  if (m_active)
    2.         {
    3.  
    4.             transform.Translate(Vector3.right * speed * Time.deltaTime);
    5.             transform.Translate(Vector3.left * speed * Time.deltaTime);
    6.             transform.Translate(Vector3.up * speed * Time.deltaTime);
    7.             transform.Translate(Vector3.down * speed * Time.deltaTime);
    8.  
    9.  airplane.transform.position = new Vector3(Mathf.Clamp(airplane.position.x, xMinA, xMaxA), Mathf.Clamp(airplane.position.y, yMinA, yMaxA), airplane.position.z);
    10. --------- etc --------
    11.          }
    But that makes it not move at all.
     
    Last edited: Jul 31, 2018
  20. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi ellenblomw,

    Just to check I understand you, this is almost going back to what we started looking into above:
    1. The box sits somewhere on the screen with the world in view behind.
    2. Click on the box to 'grab' it.
    3. Then, by moving the mouse, let's say to the right, the world would move to the left with the box remaining stationery on the screen.
    4. This would continue to the limits of the world. In particular- the mouse cannot be constrained by the screen limits if the world itself extends beyond those limits.
    Is that what you are wanting?
     
  21. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    "
    1. Then, by moving the mouse, let's say to the right, the world would move to the left with the box remaining stationery on the screen.
    "
    Not exactly, it would rather move the box around on the screen and when getting close to the edges the camera would follow in that direction so the box moves around in the world (not main camera since that is attached to the airplane, I made an extra camera with this script
    Code (CSharp):
    1. [SerializeField]
    2.     private float xMin;
    3.     [SerializeField]
    4.     private float xMax;
    5.     [SerializeField]
    6.     private float yMin;
    7.     [SerializeField]
    8.     private float yMax;
    9.  
    10.     public GameObject player;       //Public variable to store a reference to the player game object
    11.     public Transform targetCam;
    12.     private Vector3 offset;         //Private variable to store the offset distance between the player and camera
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         //Calculate and store the offset value by getting the distance between the player's position and camera's position.
    18.         offset = transform.position - player.transform.position;
    19.  
    20.     }
    21.  
    22.     // LateUpdate is called after Update each frame
    23.     void LateUpdate()
    24.     {
    25.         // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
    26.         transform.position = player.transform.position + offset;
    27.      
    28.         // Set boundaries for camera
    29.         transform.position = new Vector3(Mathf.Clamp(targetCam.position.x, xMin, xMax), Mathf.Clamp(targetCam.position.y, yMin, yMax), -25);
    30.  
    31.     }
    ).

    I have also tried replacing your script with my original posted script (first post in this thread), but I get a problem with speed again. The box is as fast as the speed of light.
     
    Last edited: Jul 31, 2018
  22. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You could be on the verge of discovering time travel!! :p

    Ok, let's start with small steps. Place your box object into the scene, make the camera be the child of it and attach the script below to it. This does not cover the moving close to the edge part but, like I said, small steps :-
    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.     void OnMouseDown()
    4.     {
    5.         Cursor.lockState = CursorLockMode.Locked;
    6.         m_active = true;
    7.     }
    8.  
    9.     void OnMouseUp()
    10.     {
    11.         Cursor.lockState = CursorLockMode.None;
    12.         m_active = false;
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if(m_active)
    18.         {
    19.             var pos = transform.position;
    20.             pos.x += Input.GetAxis(mouseX) * speed * Time.deltaTime;
    21.             pos.y += Input.GetAxis(mouseY) * speed * Time.deltaTime;
    22.             transform.position = pos;
    23.         }
    24.     }
    25.  
    26.     const string mouseX = "Mouse X";
    27.     const string mouseY = "Mouse Y";
    28.     bool m_active = false;
    29.  
    30. #pragma warning disable 649
    31.     [SerializeField] [Range(0.5f, 5f)] int speed = 3;
    32. #pragma warning restore 649
    33. }
     
  23. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hahaha! I wish :p!

    Should I place the script to camera or box? And should I deactivate my "camera follow" script?

    right now I placed it on the box and deactivated Camera Follow script.
     
  24. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    For the moment, just place it on the box and remove your camera follow. We can then try building it up as we go. :)
     
  25. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Cool, well right now its moving around real slow with the box perfectly placed in the middle.

     
    Last edited: Jul 31, 2018
  26. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    So two things :-

    First, have you copied the script I gave as a whole? The mouse cursor should disappear for the duration of dragging the box. That is what allows you to not have to keep releasing and re-clicking on the box.

    Second, yes the box will remain stationery on the screen. Like I said, let's do this in stages. :)
     
  27. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Yes copied all of the script. I don't have to reclick :D. Just have to "redraw" on the cursor plate or whatever the name is in english. Touchpad! I meant I have to keep dragging my finger on the touchpad (lifting, moving back etc).
    And the mouse cursor dissappears just as you write
     
  28. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, so now, if you don't have the camera parented to the box, the box should move around the screen instead, right?
     
  29. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    That is correct :D
     
  30. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Cool. So now I guess what you could do is, when the box approaches the edge of the screen, parent the camera to the box and then un-parent it when the box moves away from the edge. Would that work for you?
     
  31. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    I can't see why it wouldn't, but probably have to try it to see if it works. Would it be something like calling the screen width and height and then checking for box position in relation to this. For instance if position is > screen width , anchor camera?

    Also its moving really slow even if I change "speed" to 5.
     
  32. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Basically, yes:
    if( position.x > screen_width - boundary_size )


    That is odd. I have speed set to 3, with the camera not parented to the box, it seems to move ok. But I have a smaller game scene than you do. Not sure if that makes a difference or not. What if you take the speed to 10 or 100? Presumably, there must be some value that works.
     
  33. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Movement speed doesn't change when I change speed, I even tried 100. Very weird.

    [SerializeField] [Range(0.5f, 100f)] int speed = 100;
     
  34. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You will have to change it in the Editor. Either that or remove the script and then add it back again. The reason is that once a value has been serialised by Unity, that value will remain until it is changed in the Editor (or the variable gets removed).
     
  35. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Yes of course, how stupid of me! Will give it a go in the editor, but I understand what you mean and I'm sure it will work.

    Added
    Code (CSharp):
    1.     public Camera cam;
    2.     private int width;
    3.  
    4.     void Start() {
    5.        width = Screen.width;
    6.     }
    And

    Code (CSharp):
    1.  
    2.         if (m_active)
    3.         {
    4.             var pos = transform.position;
    5.             pos.x += Input.GetAxis(mouseX) * speed * Time.deltaTime;
    6.             pos.y += Input.GetAxis(mouseY) * speed * Time.deltaTime;
    7.             transform.position = pos;
    8.          
    9.  if (transform.position.x > width)
    10.             {
    11.                 cam.transform.parent = gameObject.transform;
    12.                 Debug.Log("Worked");
    13.             }
    14.             else
    15.             {
    16.                 transform.parent = null;
    17.             }
    18.  
    Am I on the right path? Haven't gotten it to attach, but it seems it doesn't register the debug.log either.
     
  36. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    On the right path, yes. :)

    You need to convert the transform from that world space to a screen space co-ordinate. Take a look at WorldToScreenPoint.

    You will probably want
    cam.transform.parent = null;
     
  37. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Thank you very much!

    So this works:

    Code (CSharp):
    1.         if (m_active)
    2.         {
    3.             Vector3 convert = cam.WorldToScreenPoint(target.position);
    4.  
    5.             var pos = transform.position;
    6.             pos.x += Input.GetAxis(mouseX) * speed * Time.deltaTime;
    7.             pos.y += Input.GetAxis(mouseY) * speed * Time.deltaTime;
    8.             transform.position = pos;
    9.  
    10.             if (convert.x > width)
    11.             {
    12.                 cam.transform.parent = gameObject.transform;
    13.                 Debug.Log("Worked");
    14.             }
    But as soon as I add more it stops working.

    Code (CSharp):
    1.         if (m_active)
    2.         {
    3.             Vector3 convert = cam.WorldToScreenPoint(target.position);
    4.  
    5.             var pos = transform.position;
    6.             pos.x += Input.GetAxis(mouseX) * speed * Time.deltaTime;
    7.             pos.y += Input.GetAxis(mouseY) * speed * Time.deltaTime;
    8.             transform.position = pos;
    9.  
    10.             if (convert.x > width)
    11.             {
    12.                 cam.transform.parent = gameObject.transform;
    13.                 Debug.Log("Worked");
    14.             }
    15.             if (convert.x < 0)
    16.             {
    17.                 cam.transform.parent = gameObject.transform;
    18.                 Debug.Log("Worked");
    19.             }
    20.             if (convert.y > height)
    21.             {
    22.                 cam.transform.parent = gameObject.transform;
    23.                 Debug.Log("Worked");
    24.             }
    25.             if (convert.y < 0)
    26.             {
    27.                 cam.transform.parent = gameObject.transform;
    28.                 Debug.Log("Worked");
    29.             }
    30.             else
    31.             {
    32.                 cam.transform.parent = null;
    33.             }
     
    Last edited: Jul 31, 2018
  38. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I think
    Vector3 convert = cam.WorldToScreenPoint(target.position);
    should be
    Vector3 convert = cam.WorldToScreenPoint(transform.position);
    .

    Also, you probably want the minimum X and Y values to be more than zero. Otherwise it would have to go to the edge of the screen.
     
  39. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    "Also, you probably want the minimum X and Y values to be more than zero. Otherwise it would have to go to the edge of the screen."

    Wouldn't that make it a problem on different screen resolutions?
     
  40. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    This is what happens:


    I have a problem with the box dissappearing out of the screen area and also not quite sure how to handle the "minimum values" :S. Sorry to be such a n00b, but I am learning a lot :)
     
  41. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Not if you use
    Screen.width
    and
    Screen.height
    . So you could try something like this:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         m_minX = Screen.width * m_borderPct;
    4.         m_maxX = Screen.width - m_minX;
    5.  
    6.         m_minY = Screen.height * m_borderPct;
    7.         m_maxY = Screen.height - m_minY;
    8.     }
    9.  
    10.     const float m_borderPct = 0.1f;
    11.     float m_minX, m_maxX;
    12.     float m_minY, m_maxY;
     
    ellenblomw likes this.
  42. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Just another option here, you could use a Cinemachine virtual camera. They have a 2D camera with follow, smoothing, constraints, etc. all out of the box.

    I don't want to undermine all the hard work you are doing on your own camera controller, but this is just an option if you want to play with something ready to go out of the box to see if it suits your needs.
     
  43. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hi Barskey, thank you very much. I will keep trying tomorrow morning since I have worked on this so long, but if it doesn't work out I will for sure look into Cinemachine. Right now its still fun to learn some new code :p. But I guess its also good to learn some good tools.
     
    barskey likes this.