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

Question 2D Camera movement is too rough

Discussion in 'Scripting' started by zsolt_szabo, May 24, 2021.

  1. zsolt_szabo

    zsolt_szabo

    Joined:
    Jan 22, 2017
    Posts:
    6
    Hi, I m working on my camera movement but I cant figure out how to achieve smoothness in the movement, with some dumping.
    Have you any idea which method should I use ?

    Here is the base code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class moving1 : MonoBehaviour
    6. {
    7.     private Vector3 touchStart;
    8.  
    9.  
    10.  
    11.     void Start()
    12.     {
    13.      
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         if (Input.GetMouseButtonDown(0))
    19.         {
    20.             touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    21.         }
    22.  
    23.         if (Input.GetMouseButton(0))
    24.         {
    25.             Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
    26.             Camera.main.transform.position += direction;
    27.  
    28.         }
    29.     }
    30. }
    31.  
     
    Last edited: May 24, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Camera code is super-tricky to do, especially interactively.

    I recommend checking out Cinemachine from the Unity Package Manager.
     
  3. zsolt_szabo

    zsolt_szabo

    Joined:
    Jan 22, 2017
    Posts:
    6
    Yes, I know about Cinemachine, but Im not sure if this is what Im looking for, but thank you
     
  4. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    274
    You just calculate the difference in positions and add it to camera in one frame.
    If you want to make things smoother, then the easiest thing would be Lerp function, it's very useful in many situation.
    You have position A, position B, and T modifier. Lerp(A, B, T);
    T = 0 -> output A
    T = 1 -> output B
    T = 0.5f -> middle between A and B

    It's not pure linear interpolation in my case, lerp means "every frame you move the same step", in my case camera will move some friction of whole path every frame, but it's usually what you need if you want your camera to feel smooth, follow fast if player is far, follow slow if player is close.
    Code (CSharp):
    1. Vector3 finalCameraPosition;
    2. float lerpSpeed;
    3. const float CameraZ = -10;
    4.  
    5. var nextCameraPosition =
    6.     Vector3.Lerp(camera.transform.position, finalCameraPosition, lerpSpeed * Time.deltaTime);
    7.  
    8. nextCameraPosition.z = CameraZ;
    9. camera.transform.position = nextCameraPosition;
    If you want to move it by same step, then you can use Vector3.MoveTowards() function.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    You can implement your own low-pass filter system, it's fairly simple.

    Lerping to smooth things out:

    https://forum.unity.com/threads/side-to-side-movement-rotation.941483/#post-6148016

    https://forum.unity.com/threads/need-help-smoothing-out-my-mouse-look-solved.543416/#post-3583643

    In the case of input smoothing (implementing your own input filtering):

    https://forum.unity.com/threads/how...ative-horizontal-buttons.963033/#post-6271251

    https://forum.unity.com/threads/need-simple-rocket-script-like-blast-city.1013560/#post-6569128