I am attempting to create a movement system similar to HOI4 or EU4 where the camera is moved with WASD and scroll wheel to navigate a top down map. I have managed to create the WASD movement but I am confused as to how to use the scroll wheel to zoom the camera in and out. I’ve followed 3 different tutorials but none of them work. Thanks in advance for any help.
Steps to success:
-
identify how to read the scroll wheel… test it by reading the values each frame and displaying them with Debug.Log()
-
identify how your camera is offset back… this could be done many ways, but you know how you’re doing it. It might also be handled by orthographicSize if it is an ortho camera.
-
use the input from step 1 above to modify the value used in step 2 below.
Don’t forget to have a minimum and maximum in/out value defined as well.
Thanks
Could you explain what you mean in step 2? I am using the main camera the scene begins with. How would I move it in or out?
If the camera is ortho, moving it closer/farther won’t work. This is the case where you need to adjust .orthoSize property.
If the camera is perspective, then you move it closer or further away, using whatever movement mechanism you like.
NOTE: you have posted three separate times without any code or any details about your scene or your setup. As long as that continues, ALL comments back to you are going to remain deliberately vague because you have not given out anything I can get specific about.
I’m sorry, I’m new to Unity and coding c#. Thanks for all the advice.
I attempted to insert what you suggested but it still doesn’t function. I am using an orthographic camera.
The code I’m using:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_Camera : MonoBehaviour
{
private Vector3 cameraMovementPosition;
public float speed = 1f;
private object cameraMovement;
public Camera m_OrthographicCamera;
private float m_ViewPositionX, m_ViewPositionY, m_ViewWidth, m_ViewHeight;
public void Start()
{
}
private void Update()
{
HandleCameraMovement();
HandleCameraZoom();
}
public void HandleCameraZoom()
{
float zoomChangeAmount = 80f;
if (Input.mouseScrollDelta.y > 0)
{
m_ViewWidth -= zoomChangeAmount * Time.deltaTime;
m_ViewHeight -= zoomChangeAmount * Time.deltaTime;
}
if (Input.mouseScrollDelta.y < 0)
{
m_ViewWidth += zoomChangeAmount * Time.deltaTime;
m_ViewWidth += zoomChangeAmount * Time.deltaTime;
}
if (m_OrthographicCamera)
{
return;
}
m_OrthographicCamera.enabled = true;
m_OrthographicCamera.orthographic = true;
m_OrthographicCamera.orthographicSize = 5.0f;
m_OrthographicCamera.rect = new Rect(m_ViewPositionX, m_ViewPositionY, m_ViewWidth, m_ViewHeight);
}
private void HandleCameraMovement()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * Time.deltaTime * speed;
}
}
}
Thanks soooooooooooooooooo much!
I switched the type of camera from ortho to perspective then I used that tutorial and a few forum posts to devise this code. It should allow the code to zoom out and increase and decrease in zoom speed. However it does not do this and speeds up and does not decrease again when I zoom back in. Please someone save my sanity!
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_Camera : MonoBehaviour
{
private float curZoomSpeed = 1f;
private float maxCamSpeed = 300f;
private float cameraAcceleration = 20f;
public float speed = 1f;
private float maxSpeed = 25f;
private float speedacc = 1f;
private object cameraMovement;
public void Start()
{
}
private void Update()
{
HandleCameraMovement();
HandleCameraZoom();
}
public void HandleCameraZoom()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
//GetComponent<Camera> ().fieldOfView++;
GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y, transform.position.z + curZoomSpeed);
curZoomSpeed -= cameraAcceleration * Time.deltaTime;
speed -= speedacc * Time.deltaTime;
if (speed > maxSpeed)
{
speed = maxSpeed;
}
if (curZoomSpeed > maxCamSpeed)
{
curZoomSpeed = maxCamSpeed;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//GetComponent<Camera> ().fieldOfView--;
GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y, transform.position.z - curZoomSpeed);
curZoomSpeed += cameraAcceleration * Time.deltaTime;
speed += speedacc * Time.deltaTime;
if (speed > maxSpeed)
{
speed = maxSpeed;
}
if (curZoomSpeed > maxCamSpeed)
{
curZoomSpeed = maxCamSpeed;
}
}
}
private void HandleCameraMovement()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * Time.deltaTime * speed;
}
}
}
+1