There is a script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
public float moveSpeed = 3f;
void Update()
{
CameraTranslate();
}
void CameraTranslate()
{
float transX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
float transZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.position += transform.right * transX;
transform.Translate(transX, 0, transZ);
}
}
How to add mouse control? When the mouse approaches the edge of the screen, the camera moves in the same direction.
How to limit the movement of the camera along the xz axes? For example, from -4000 to 4000.
Camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.
Luckily there are tons of tutorials for integrating it already on Youtube, both from Unity and third parties.
1 Like
Thanks to your help from the previous topic, I was able to find a script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove2 : MonoBehaviour
{
public float CameraSpeed;
public float moveSpeed = 3f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
CameraWidthPositior();
CameraTranslate();
}
void CameraWidthPositior()
{
if (20 > Input.mousePosition.y)
{
transform.position -= new Vector3(CameraSpeed, 0, 0);
}
if ((Screen.width - 10) < Input.mousePosition.x)
{
transform.position -= new Vector3(0, 0, CameraSpeed);
}
if (20 > Input.mousePosition.x)
{
transform.position +=new Vector3(0, 0, CameraSpeed);
}
if ((Screen.height - 10) < Input.mousePosition.y)
{
transform.position +=new Vector3(CameraSpeed, 0, 0);
}
}
void CameraTranslate()
{
float transX = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
float transZ = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.position += transform.right * transX;
transform.Translate(transX, 0, transZ);
}
}
Now we need to figure out how to limit the movement of the camera.
I’ve done everything.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove4 : MonoBehaviour
{
public float CameraSpeed = 3f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
CameraWidthPositior();
CameraTranslate();
}
void CameraTranslate()
{
if ( transform.position.x > -4800 & transform.position.x < -800 & Input.GetAxis("Vertical")==0)
{
float transX = Input.GetAxis("Horizontal") * CameraSpeed * Time.deltaTime;
transform.Translate(transX, 0, 0);
}
else if (Input.GetAxis("Horizontal")>=0 & transform.position.x < -800)
{
float transX = Input.GetAxis("Horizontal") * CameraSpeed * Time.deltaTime;
transform.Translate(transX, 0, 0);
}
else if (Input.GetAxis("Horizontal")<= 0 & transform.position.x > -4800 )
{
float transX = Input.GetAxis("Horizontal") * CameraSpeed * Time.deltaTime;
transform.Translate(transX, 0, 0);
}
if (transform.position.y > 943 & transform.position.y < 2650 & Input.GetAxis("Horizontal") == 0 )
{
float transZ = Input.GetAxis("Vertical") * CameraSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.Translate(0, 0, transZ);
}
else if (Input.GetAxis("Vertical") >= 0 & transform.position.y > 943)
{
float transZ = Input.GetAxis("Vertical") * CameraSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.Translate(0, 0, transZ);
}
else if (Input.GetAxis("Vertical") <= 0 & transform.position.y < 2650)
{
float transZ = Input.GetAxis("Vertical") * CameraSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.Translate(0, 0, transZ);
}
}
void CameraWidthPositior()
{
if (20 > Input.mousePosition.y & transform.position.y < 2650)
{
float transZ = -CameraSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.Translate(0, 0, transZ);
}
if ((Screen.width - 10) < Input.mousePosition.x & transform.position.x < -800)
{
transform.Translate(CameraSpeed * Time.deltaTime, 0, 0);
}
if (20 > Input.mousePosition.x & transform.position.x > -4800)
{
transform.Translate(-CameraSpeed * Time.deltaTime, 0, 0);
}
if ((Screen.height - 10) < Input.mousePosition.y & transform.position.y > 943)
{
float transZ = CameraSpeed * Time.deltaTime;
var f = Vector3.Cross(transform.right, Vector3.up).normalized;
transform.position += f * transZ;
transform.Translate(0, 0, transZ);
}
}
}