Rayo75
Rayo75

Reputation: 11

How do i make it into a smooth camera? (unity C# programming)

This is what i got and why does this website want me to put more explaining when i clarified above well i want to achieve a smooth camera control where the player is in the middle of the camera if the mouse is in the middle but as it moves toward a direction i want the camera to move just a little bit to reveal terrain (here i have 2 problems: 1.The camera doesnt move if the mouse is at the point 0,0 of the screen and i want it to be the center of the camera 2. the camera drifts away in that direction, it doesnt stop like i want to make it):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

   // Camera cam = Camera.main.orthographicSize;
    //Vector2 camcenter = new Vector2(cam * 2, camheight);

    public GameObject mage;
    private Vector3 offset;
    public float mincam = 30f;
    public float maxcam = 120f;
    public bool mouse_smooth_cam = false;

    // Update is called once per frame
    void LateUpdate ()
    {
        offset = Input.mousePosition / 100 + transform.position - mage.transform.position;
        transform.position = mage.transform.position + offset;
    }

    void Update()
    {
        HandleZoom();
    }

    private void HandleZoom()
    {
        float scrollValue = Input.mouseScrollDelta.y;

        float newCamSize = Camera.main.orthographicSize - scrollValue*4;
        Camera.main.orthographicSize = Mathf.Clamp(newCamSize, mincam, maxcam);
    }
}

Upvotes: 0

Views: 134

Answers (1)

Bizhan
Bizhan

Reputation: 17085

Instead of Input.mousePosition / 100 use Camera.main.ScreenToWorldPoint(Input.mousePosition)

also always use Time.deltaTime in Update methods

var PAN_SPEED = 30f;
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offset = mousePos + transform.position - mage.transform.position;
transform.position = mage.transform.position + offset * Time.deltaTime * PAN_SPEED;

Upvotes: 2

Related Questions