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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera movement script not working properly

Discussion in 'Scripting' started by Corva-Nocta, Nov 5, 2015.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Code (csharp):
    1.  
    2. {
    3.     public Transform target;
    4.  
    5.     public float height = 7;
    6.  
    7.     public float radius = 7;
    8.  
    9.     public float angle = 0;
    10.     public float rotationalSpeed = 1;
    11.    
    12.     void Update ()
    13.     {
    14.         float cameraX = target.position.x + (radius * Mathf.Cos(angle));
    15.         float cameraY = target.position.y + height;
    16.         float cameraZ = target.position.z + (radius * Mathf.Sin(angle));
    17.  
    18.         transform.position = new Vector3(cameraX, cameraY, cameraZ);
    19.  
    20.         if(Input.GetKey(KeyCode.A))
    21.         {
    22.             angle = angle - rotationalSpeed + Time.deltaTime;
    23.         }
    24.         else if(Input.GetKey(KeyCode.D))
    25.         {
    26.             angle = angle + rotationalSpeed + Time.deltaTime;
    27.         }
    28.  
    29.         transform.LookAt(target.position);
    30.     }
    31. }
    32.  
    This is the script I am working with but there are a few small issues with it.

    1.) For some reason if the rotate speed is set to 1 and I press the key to turn the camera it moves extremely fast and far, the transition is not smooth at all. In fact its only about as smooth as I want it if I set the speed down to 0.05 which doesn't seem right to me. Am I doing something wrong?

    2.) when using the A key to rotate the camera it moves at one speed, but when I use the D key it moves at a much faster speed, about twice as fast or more. As far as I can tell this shouldn't happen but it is, no clue why.

    3.) if I want to change the keys for A and D to the arrow keys, what code do I use?

    Any help at all on these issues would be great! Thanks
     
  2. Wowo51

    Wowo51

    Joined:
    Oct 12, 2015
    Posts:
    25
    You are adding the time, p=vt, multiplying should take care of 1 and 2. As for 3, in Visual Studio you can get a list of everything 'under the dot' by deleting the .A after KeyCode temporarily and pressing the period key. You should see a list of options popup.
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I am assuming I am switching line:
    angle = angle - rotationalSpeed +Time.deltaTime;

    ah ok handy, did not know that. I knew it was something simple haha. Thanks a bunch!
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Both worked like a charm! Thanks a ton!