Search Unity

Looking for help on controlling camera with cinemachine moved by player

Discussion in 'Cinemachine' started by arozwalak, Jan 1, 2021.

  1. arozwalak

    arozwalak

    Joined:
    Dec 27, 2020
    Posts:
    1
    hi guys, I'm quite new in unity and I'm stuck on controlling camera movement by player. As an exercise I'm creating kind of RTS game where player can move a camera above the map using W S A D and controlling camera rotation when holding right mouse button and move mouse.

    For this I created empty game object which is followed by camera, user is moving this object using WSAD.
    I'm trying to limit movement of this object between 0 and 1000 on X and Z axis, but I'm struggling with that if player rotate this object to any other angle


    I've created 4 objects with BoxCollider on each edge, but I'm having problem write a script to prevent user to leave that area.



    Not sure if that's even right direction or is there any better way for this?

    Before adding BoxColliders I was trying to calculate box position and prevent update if new X or Z position is outside boundary, which was working fine if I'm facing 'Forward', but once turn camera 90 or 180 degree when WSAD is moving oposit direction this kind of script doesn't work anymore

    My script

    Code (CSharp):
    1.  
    2. private void Update()
    3.     {
    4.         _horizontalInput = Input.GetAxis("Horizontal");
    5.         _verticalInput = Input.GetAxis("Vertical");
    6.         _mouseXInput = Input.GetAxis("Mouse X");
    7.         _mouseYInput = -Input.GetAxis("Mouse Y");
    8.         _rightMouseBtnClicked = Input.GetMouseButton(1);
    9.  
    10.         // rotate camera with mouse when right mouse button is hold
    11.         if (_rightMouseBtnClicked)
    12.         {
    13.             // rotate camera horizontally
    14.             transform.Rotate(Vector3.up * Time.deltaTime * _cameraSpeed * _mouseXInput);
    15.         }
    16.  
    17.         Vector3 vectRight = Vector3.right * Time.deltaTime * _cameraSpeed * _horizontalInput;
    18.         Vector3 vectForward = Vector3.forward * Time.deltaTime * _cameraSpeed * _verticalInput;
    19.         Vector3 _boxNewPos = vectRight + vectForward;
    20.  
    21.         float posX = transform.position.x;
    22.         float posZ = transform.position.z;
    23.  
    24.         float deltaX = posX + _boxNewPos.x;
    25.         float deltaZ = posZ + _boxNewPos.z;
    26.         Debug.Log(deltaX + " " + deltaZ);
    27.         Debug.Log(posX + " " + posZ);
    28.  
    29.         if (deltaX < posX && posX > 100)
    30.         {
    31.             transform.Translate(new Vector3(_boxNewPos.x, 0, 0));
    32.         }
    33.  
    34.         if (deltaX > posX && posX < 800)
    35.         {
    36.             transform.Translate(new Vector3(_boxNewPos.x, 0, 0));
    37.         }
    38.  
    39.  
    40.         if (deltaZ < posZ && posZ > 100)
    41.         {
    42.             transform.Translate(new Vector3(0, 0, _boxNewPos.z));
    43.         }
    44.  
    45.         if (deltaZ > posZ && posZ < 800)
    46.         {
    47.             transform.Translate(new Vector3(0, 0, _boxNewPos.z));
    48.         }
    49.     }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Try something like this, instead of using transform.Translate:
    Code (CSharp):
    1.             Vector3 delta = vectRight + vectForward;
    2.             Vector3 pos = transform.position;
    3.             Vector3 newPos = pos + delta;
    4.  
    5.             // Clamp to inside the bounds
    6.             if (newPos.x > 100)
    7.                 newPos.x = 100;
    8.             // etc for the other boundaries
    9.  
    10.             transform.position = newPos;