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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Influencing Transforms

Discussion in 'Scripting' started by Lastonestanding, May 21, 2022.

  1. Lastonestanding

    Lastonestanding

    Joined:
    Nov 20, 2013
    Posts:
    10
    Hello New to C# and Unity Coding, Working on a RTS Camera, but none of the Tutorials I am which explain how to do one thing I am wanting to do with my Camera. On the Transform, when a player zooms in I am wanting to make the X Rotation axis get to a min of 5, but as the player Zooms out the Axis goes to amount of 50. making looks like this:

    Zoomed Out
    https://ibb.co/q0VDLBM

    Zoomed In:
    https://ibb.co/581Kd3C

    I know I could make a float and get it to inform the transform, but nor sure how to put this in code. Would it be :

    transform.postion.x = MaxHeightX (MaxHeightX being the float) Is this how I would do it, I will keep trying but if anyone knows please explain it to me. Thank you for your Time.
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    because position is a struct type you will need to first cache it locally, same thing will be true for rotations as well.
    Code (CSharp):
    1. var pos = transform.position;
    2. pos.x = MaxHeightX;
    3. transform.postion = pos;
     
    Last edited: May 21, 2022
    Lastonestanding likes this.
  3. Lastonestanding

    Lastonestanding

    Joined:
    Nov 20, 2013
    Posts:
    10
    Thank you been trying, however realized I have been meaning to be asking for the Rotation not the Position. My mistake, (Still New to Understanding this) But so far not connecting here is the code I have been doing at the Moment. I will keep looking into this.

    Code (CSharp):
    1. public class CameraMovement : MonoBehaviour
    2. {
    3.     float speed = 0.06f;
    4.     float zoomSpeed = 10.0f;
    5.     float rotateSpeed = 0.1f;
    6.  
    7.     float maxHeight = 65f;
    8.     float minHeight =  3f;
    9.  
    10.     float XposHH = 50f;
    11.     float XposHL = 3f;
    12.  
    13.     Vector2 p1;
    14.     Vector2 p2;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.        
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.  
    27.         if (Input.GetKey(KeyCode.LeftShift))
    28.         {
    29.             speed = 0.06f;
    30.             zoomSpeed = 20.0f;
    31.         }
    32.         else
    33.         {
    34.             speed = 0.035f;
    35.             zoomSpeed = 10.0f;
    36.         }
    37.  
    38.         float hsp = transform.position.y * speed * Input.GetAxis("Horizontal");
    39.         float vsp = transform.position.y * speed * Input.GetAxis("Vertical");
    40.         float scrollSp =  -zoomSpeed * Input.GetAxis("Mouse ScrollWheel");
    41.         float scrollAngle = transform.rotation.x * speed * Input.GetAxis("Mouse ScrollWheel");
    42.  
    43.  
    44.         if ((transform.position.y >= maxHeight) && (scrollSp > 0))
    45.         {
    46.             scrollSp = 0;
    47.         }
    48.         else if ((transform.position.y <= minHeight) && (scrollSp < 0))
    49.         {
    50.             scrollSp = 0;
    51.         }
    52.  
    53.         if ((transform.position.y + scrollSp) > maxHeight)
    54.         {
    55.             scrollSp = maxHeight - transform.position.y;
    56.         }
    57.         else if ((transform.position.y + scrollSp) < minHeight)
    58.         {
    59.             scrollSp = minHeight - transform.position.y;
    60.         }
    61.  
    62.  
    63.         if ((transform.rotation.x >= XposHH) && (scrollAngle > 0))
    64.         {
    65.             scrollAngle = 50;
    66.         }
    67.         else if ((transform.rotation.x <= XposHL) && (scrollAngle < 0))
    68.         {
    69.             scrollAngle = 3;
    70.         }
    71.  
    72.  
    73.  
    74.         Vector3 verticalMove = new Vector3(0, scrollSp, 0);
    75.         Vector3 lateralMove = hsp * transform.right;
    76.         Vector3 forwardMove = transform.forward;
    77.         forwardMove.y = 0;
    78.         forwardMove.Normalize();
    79.         forwardMove *= vsp;
    80.  
    81.         Vector3 move = verticalMove + lateralMove + forwardMove;
    82.  
    83.         transform.position += move;
    84.  
    85.         getCameraRotation();
    86.     }
    87.  
    88.     void getCameraRotation()
    89.     {
    90.         if (Input.GetMouseButtonDown(2))
    91.         {
    92.             p1 = Input.mousePosition;
    93.         }
    94.  
    95.         if (Input.GetMouseButton(2))
    96.         {
    97.             p2 = Input.mousePosition;
    98.  
    99.             float dx = (p1 - p2).x * rotateSpeed;
    100.             float dy = (p2 - p1).y * rotateSpeed;
    101.  
    102.             transform.rotation *= Quaternion.Euler(new Vector3(0, dx, 0));
    103.         }
    104.     }
    105. }
    106.  
     
  4. Lastonestanding

    Lastonestanding

    Joined:
    Nov 20, 2013
    Posts:
    10
    UPDATE!!!: Figured Out it the transform.eulerAngles, I needed. But Not only Snaps to the Positions once I reach the highest or lowest points. Now the question what is the argument to make the Camera change angle gradually as you zoom in and out, if anyone can explain this to me let me know. Here is what I did so far:

    Code (CSharp):
    1. public class CameraMovement : MonoBehaviour
    2. {
    3.     float speed = 0.06f;
    4.     float zoomSpeed = 10.0f;
    5.     float rotateSpeed = 0.1f;
    6.  
    7.     float maxHeight = 65f;
    8.     float minHeight =  3f;
    9.  
    10.     float XposHH = 50f;
    11.     float XposHL = 3f;
    12.  
    13.  
    14.     Vector2 p1;
    15.     Vector2 p2;
    16.  
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.        
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.         if (Input.GetKey(KeyCode.LeftShift))
    29.         {
    30.             speed = 0.06f;
    31.             zoomSpeed = 20.0f;
    32.         }
    33.         else
    34.         {
    35.             speed = 0.035f;
    36.             zoomSpeed = 10.0f;
    37.         }
    38.  
    39.         float hsp = transform.position.y * speed * Input.GetAxis("Horizontal");
    40.         float vsp = transform.position.y * speed * Input.GetAxis("Vertical");
    41.         float scrollSp =  -zoomSpeed * Input.GetAxis("Mouse ScrollWheel");
    42.  
    43.  
    44.  
    45.         if (((transform.position.y >= maxHeight)) && (scrollSp > 0))
    46.         {
    47.             scrollSp = 0;
    48.             transform.eulerAngles = new Vector3(XposHH, 0, 0);
    49.         }
    50.         else if ((transform.position.y <= minHeight) && (scrollSp < 0))
    51.         {
    52.             scrollSp = 0;
    53.             transform.eulerAngles = new Vector3(XposHL, 0, 0);
    54.         }
     
  5. Lastonestanding

    Lastonestanding

    Joined:
    Nov 20, 2013
    Posts:
    10
    UPDATE: I got it to work had to use the eulerAngles to make the Camera do it's angles!!! I will show how I did it tomorrow.