Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Camera snaps to position instead of sliding to position!!

Discussion in 'Scripting' started by TheShinigami6, Jan 24, 2021.

  1. TheShinigami6

    TheShinigami6

    Joined:
    Jan 19, 2021
    Posts:
    4
    Hey, I am trying to make the mouse control the camera's rotation, but instead of sliding around the pivot point, it snaps around the pivot point, from one location to another, much like its teleporting, my script is below :-
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class camcontrol : MonoBehaviour
    6. {
    7.     public float rotationX;
    8.     public float sensitivity = 500f;
    9.     public float mouseY;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {  
    20.         mInput();  
    21.     }
    22.  
    23.     void FixedUpdate()
    24.     {
    25.         camRot();
    26.     }
    27.  
    28.     void mInput()
    29.     {
    30.         //Taking input
    31.         mouseY = Input.GetAxis("Mouse Y") * sensitivity;
    32.     }
    33.  
    34.     void camRot()
    35.     {
    36.         //Rotate Cam up and down
    37.         rotationX -= mouseY;
    38.         rotationX = Mathf.Clamp(rotationX, -90f, 90f);
    39.         transform.localEulerAngles = new Vector3(rotationX, 0f, 0f);
    40.     }
    41. }
    42.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Yes, that happens becouse you dont multiply your rotation value *Time.deltaTime();

    here in this video you can take a look how the Character gets rotated,
    it should work the same for your camera

     
  3. TheShinigami6

    TheShinigami6

    Joined:
    Jan 19, 2021
    Posts:
    4
    My movement is better but still not fluid, here is the code :-

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class camcontrol : MonoBehaviour
    5. {
    6.     public float rotationX;
    7.     public float sensitivity = 1500f;
    8.     public float mouseY;
    9.     public float mouseX;
    10.     public bool isCursorLocked;
    11.     bool Esc;
    12.     public Transform playerbody;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.         isCursorLocked = true;
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         mInput();
    23.     }
    24.     void LateUpdate()
    25.     {
    26.         camRot();
    27.     }
    28.     void mInput()
    29.     {
    30.         //Taking input
    31.         mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
    32.         mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
    33.  
    34.     }
    35.     void camRot()
    36.     {
    37.         //Rotate Cam up and down
    38.         rotationX -= mouseY;
    39.  
    40.  
    41.         rotationX = Mathf.Clamp(rotationX, -90f, 90f);
    42.         transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
    43.  
    44.         playerbody.Rotate(Vector3.up, mouseX);
    45.  
    46.         if (Esc)
    47.         {
    48.             Cursor.lockState = CursorLockMode.None;
    49.             isCursorLocked = false;
    50.         }
    51.     }
    52. }