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. Dismiss Notice

Help me out with camera movement

Discussion in 'Getting Started' started by eclipse130300, Mar 11, 2020.

  1. eclipse130300

    eclipse130300

    Joined:
    Dec 6, 2019
    Posts:
    32
    Hello everyone! I have a pretty dumb question, although I am not so dummy at Unity.
    I have to make mobile app, and my camera has to float over map with swipes, almost like in WC3(
    with the exception of mobile Input). Unfortunatelly, my redmi doesn't support Unity Remote, so I was forced to use PC input for now(and imitate swipe scrolling with mouse movement).
    Code (CSharp):
    1. public class CameraController : MonoBehaviour
    2. {
    3.     public GameObject hitIndicator;
    4.     private Camera _camera;
    5.     public float movementSpeed;
    6.     Vector3 mapClickPoint;
    7.     Vector3 moveDirection;
    8.     private void Awake()
    9.     {
    10.         _camera = GetComponent<Camera>();
    11.     }
    12.  
    13.  
    14.     void LateUpdate()
    15.     {
    16.         Vector2 mousePos = Input.mousePosition;
    17.  
    18.         if(Input.GetMouseButtonDown(0))
    19.         {
    20.             Ray ray = _camera.ScreenPointToRay(mousePos);
    21.             RaycastHit hit;
    22.             if (Physics.Raycast(ray, out hit))
    23.             {
    24.                 mapClickPoint = new Vector3(hit.point.x, 0, hit.point.z);
    25.             }
    26.         }
    27.         if(Input.GetMouseButton(0))
    28.         {
    29.             Ray ray2 = _camera.ScreenPointToRay(mousePos);
    30.             RaycastHit hit;
    31.             if (Physics.Raycast(ray2, out hit))
    32.             {
    33.                 Vector3 mapHoverPoint = new Vector3(hit.point.x, 0, hit.point.z);
    34.                 moveDirection = (mapClickPoint - mapHoverPoint).normalized;
    35.                 transform.position += moveDirection * movementSpeed * Time.deltaTime;
    36.             }
    37.         }
    38.     }
    Here is my code, and issue is jittering on camera witch happens at times, when camera doesn't move any longer, and I hold leftclickButton. I guees the cause is the fact that I want to change position when it's not the proper time. Also I wonder, is my code might be easily converted to mobile input, and is it better to just change everythig for mobile input immideately?
     
    Last edited: Mar 11, 2020
  2. eclipse130300

    eclipse130300

    Joined:
    Dec 6, 2019
    Posts:
    32
    Alright guys, everything works when I added a line at the end
    Code (CSharp):
    1.                 if (Vector3.Distance(mapClickPoint, mapHoverPoint) >= 0.1f)
    2.                 {
    3.                     transform.position += moveDirection * movementSpeed * Time.deltaTime;
    4.                 }
    But anyway, I don't think it is a performant way to solve this issue, and furthermore, I would appreciate any piece of advice how to change this code for mobile input
     
  3. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Change LateUpdate() to Update(). Mouse and keyboard reading should be done in Update() and never in LateUpdate(). LateUpdate is used for physics movement which you are not using. LateUpdate() can be called many times a frame or not in a frame at all.
     
    eclipse130300 likes this.
  4. eclipse130300

    eclipse130300

    Joined:
    Dec 6, 2019
    Posts:
    32
    Thanks! As I understand, late update called after main game loop and drawing finishes. Maybe you meant fixed update?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're confusing LateUpdate with FixedUpdate. LateUpdate is run once per frame after Update, and is often used for things like camera positioning which needs to occur after an object it follows has moved.
     
  6. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    You are absolutely right. I misread, my bad.