Search Unity

Camera frame flicker when turning camera and then moving problem.

Discussion in 'Scripting' started by michalchajdus, Aug 18, 2019.

  1. michalchajdus

    michalchajdus

    Joined:
    Aug 17, 2019
    Posts:
    3
    Hi, i started a new project to improve and learn new stuff as a beginner in unity. I started from scratch making camera and player movement srcipts but i ran into a problem with camera. Whenever i turn it around without moving my character and then press a key to move it flickers for a frame or so. It is enough to notice that so it's annoying. Any suggestions what it might be?
    Video of the problem:


    Scripts that i use (camera is attached to empty gameObject):

    Camera Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraBaseScript : MonoBehaviour
    6. {
    7.     //variables
    8.  
    9.     private GameObject playerfollow;
    10.     private GameObject player;
    11.  
    12.     private Animator anim;
    13.  
    14.     [SerializeField]
    15.     private float sensitivity = 250.0f;
    16.     [SerializeField]
    17.     private float clampAngle = 80.0f;
    18.     [SerializeField]
    19.     private float cameraMoveSpeed = 120.0f;
    20.  
    21.     private float mouseX;
    22.     private float mouseY;
    23.     private float rotX = 0.0f;
    24.     private float rotY = 0.0f;
    25.  
    26.  
    27.     //methods
    28.     private void turnOffMouse()
    29.     {
    30.         Cursor.lockState = CursorLockMode.Locked;
    31.         Cursor.visible = false;
    32.     }
    33.  
    34.     private void getInput()
    35.     {
    36.         mouseX = Input.GetAxis("Mouse X");
    37.         mouseY = Input.GetAxis("Mouse Y");
    38.     }
    39.  
    40.     private void rotateCamera()
    41.     {
    42.         rotX += mouseX * sensitivity * Time.deltaTime;
    43.         rotY += mouseY * sensitivity * Time.deltaTime;
    44.  
    45.         rotY = Mathf.Clamp(rotY, -clampAngle, clampAngle);
    46.  
    47.         Quaternion localRotation = Quaternion.Euler(-rotY, rotX, 0);
    48.         transform.rotation = localRotation;
    49.  
    50.        
    51.         // it makes character to folllow direction of camera when moving without turning character around every camera movement
    52.         if(anim.GetFloat("vert") != 0 || anim.GetFloat("horiz") != 0)
    53.         {
    54.             player.transform.localEulerAngles = new Vector3(player.transform.rotation.eulerAngles.x, transform.eulerAngles.y, player.transform.eulerAngles.z);
    55.         }
    56.        
    57.  
    58.     }
    59.  
    60.     private void cameraOrbit()
    61.     {
    62.         float step = cameraMoveSpeed;
    63.         transform.position = Vector3.MoveTowards(transform.position, playerfollow.transform.position, step);
    64.     }
    65.  
    66.  
    67.  
    68.     //start function
    69.     void Start()
    70.     {
    71.         turnOffMouse();
    72.         rotX = transform.localRotation.eulerAngles.x;
    73.         rotY = transform.localRotation.eulerAngles.y;
    74.         player = GameObject.Find("Player");
    75.         playerfollow = GameObject.Find("CameraFollow");
    76.         anim = GetComponentInParent<Animator>();
    77.     }
    78.  
    79.     //update function **FixedUpdate makes camera look like it feels less smooth**
    80.     void Update()
    81.     {
    82.         getInput();
    83.         rotateCamera();
    84.  
    85.     }
    86.  
    87.     //LateUpdate
    88.     private void LateUpdate()
    89.     {
    90.         cameraOrbit();
    91.     }
    92.  
    93. }
    94.  
    Player script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerScript : MonoBehaviour
    6. {
    7.     //variables
    8.  
    9.     private Transform player;
    10.     private Rigidbody rb;
    11.     private Collider playerCollider;
    12.     private Animator anim;
    13.     private GameObject cameraBase;
    14.     private Camera mainCamera;
    15.  
    16.     private float horizontal;
    17.     private float vertical;
    18.  
    19.  
    20.     [SerializeField]
    21.     private float movementspeed = 10.0f;
    22.     [SerializeField]
    23.     private float jumpheight = 4.0f;
    24.     [SerializeField]
    25.     private float jumpspeed = 1.2f;
    26.     [SerializeField]
    27.     private bool doublejump = false;
    28.  
    29.     private float cameraClampMin = 60;
    30.     private float cameraClapmpMax = 90;
    31.  
    32.     int jumpcount = 0;
    33.  
    34.     //methods
    35.  
    36.     private void getInput()
    37.     {
    38.         horizontal = Input.GetAxis("Horizontal");
    39.         vertical = Input.GetAxis("Vertical");
    40.     }
    41.  
    42.     private void move()
    43.     {
    44.         player.Translate(horizontal * movementspeed * Time.deltaTime, 0,  vertical * movementspeed * Time.deltaTime, cameraBase.transform);
    45.     }
    46.  
    47.     bool isGrounded()
    48.     {
    49.         float disttoground;
    50.         disttoground = playerCollider.bounds.extents.y;
    51.         return Physics.Raycast(player.position, -Vector3.up, disttoground + 0.1f);
    52.     }
    53.  
    54.     private void runcheck()
    55.     {
    56.         anim.SetFloat("vert", vertical);
    57.         anim.SetFloat("horiz", horizontal);
    58.     }
    59.  
    60.     private void jump()
    61.     {
    62.  
    63.         if(isGrounded() && jumpcount > 1)
    64.         {
    65.             jumpcount = 0;
    66.         }
    67.  
    68.  
    69.         if (Input.GetKeyDown(KeyCode.Space) && doublejump == true && jumpcount < 2)
    70.         {
    71.             rb.AddForce(new Vector3(0, jumpheight, 0) * jumpspeed, ForceMode.Impulse);
    72.             jumpcount++;
    73.         }
    74.         else if (Input.GetKeyDown(KeyCode.Space) && isGrounded())
    75.         {
    76.             rb.AddForce(new Vector3(0, jumpheight, 0) * jumpspeed, ForceMode.Impulse);
    77.             jumpcount++;
    78.         }
    79.  
    80.     }
    81.  
    82.     private void sprint()
    83.     {
    84.  
    85.         if (Input.GetKey(KeyCode.LeftShift))
    86.         {
    87.             mainCamera.fieldOfView = Mathf.Clamp(mainCamera.fieldOfView, cameraClampMin, cameraClapmpMax);
    88.             mainCamera.fieldOfView = 90;
    89.             movementspeed = 10;
    90.         }
    91.         else
    92.         {
    93.             mainCamera.fieldOfView = Mathf.Clamp(mainCamera.fieldOfView, cameraClampMin, cameraClapmpMax);
    94.             mainCamera.fieldOfView = 60;
    95.             movementspeed = 5;
    96.         }
    97.     }
    98.  
    99.     // Start
    100.    
    101.     private void Start()
    102.     {
    103.         rb = GetComponent<Rigidbody>();
    104.         player = GetComponent<Transform>();
    105.         playerCollider = GetComponent<Collider>();
    106.         anim = GetComponent<Animator>();
    107.         cameraBase = GameObject.Find("CameraBase");
    108.         mainCamera = GetComponentInChildren<Camera>();
    109.     }
    110.  
    111.     // Update
    112.     void Update()
    113.     {
    114.         getInput();
    115.         move();
    116.         runcheck();
    117.         jump();
    118.         sprint();
    119.  
    120.     }
    121. }
    122.  
     
  2. michalchajdus

    michalchajdus

    Joined:
    Aug 17, 2019
    Posts:
    3
    I found that the solution was simply code in a wrong order. If you look at rotateCamera() function in Camera Script, I should first calculate player rotation with that if statement and then do other calculations so the function looks like this now:
    Code (CSharp):
    1. private void rotateCamera()
    2.     {
    3.         // it makes character to folllow direction of camera when moving without turning character around every camera movement
    4.         if (anim.GetFloat("vert") != 0 || anim.GetFloat("horiz") != 0)
    5.         {
    6.             player.transform.localEulerAngles = new Vector3(player.transform.rotation.eulerAngles.x, transform.eulerAngles.y, player.transform.eulerAngles.z);
    7.         }
    8.  
    9.         rotX += mouseX * sensitivity * Time.deltaTime;
    10.         rotY += mouseY * sensitivity * Time.deltaTime;
    11.  
    12.         rotY = Mathf.Clamp(rotY, -clampAngle, clampAngle);
    13.  
    14.         Quaternion localRotation = Quaternion.Euler(-rotY, rotX, 0);
    15.         transform.rotation = localRotation;
    16.     }
     
    Yoreki likes this.