SoFunction
Updated on 2025-03-01

Unity3D realizes the control of camera movement

This article shares the specific code for Unity3D to control camera movement for your reference. The specific content is as follows

Recently, the company's project development content is basically the same, and many scripts can be copied directly to splice the project. I have always been a code lover before, and I will never copy and paste if I can type it by myself. However, the development speed has indeed been delayed, so I plan to post all the commonly used scripts in development to the blog. Take it directly when you need it. Hope it helps you too.

Hold down the right mouse button in the unity editor, and you can freely control the field of view through the wasdqe key on the control keyboard.

The following is the code to implement the operation:

using ;
using ;
using UnityEngine;
 
//Cruise mode camera controlpublic class CameraMove : MonoBehaviour
{
 
 public static CameraMove Instance = null;
 
 private Vector3 dirVector3;
 private Vector3 rotaVector3;
 private float paramater = 0.1f;
 //Rotation parameters private float xspeed = -0.05f;
 private float yspeed = 0.1f;
 
 private float dis;
 
 void Awake ()
 {
 Instance = this;
 }
 
 private void Start()
 {
 rotaVector3 = ;
 dis = ;
 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
 //Rotation if ((1))
 {
   += ("Horizontal") * yspeed;
   += ("Vertical") * xspeed;
   = (rotaVector3);
 }
 
 //move dirVector3 =;
 
 if (())
 {
  if(())  = 3;
  else  = 1;
 }
 if (())
 {
  if (())  = -3;
  else  = -1;
 }
 if (())
 {
  if (())  = -3;
  else  = -1;
 }
 if (())
 {
  if (())  = 3;
  else  = 1;
 }
 if (())
 {
  if (())  = -3;
  else  = -1;
 }
 if (())
 {
  if (())  = 3;
  else  = 1;
 }
 (dirVector3 * paramater,);
 //Limit the camera range  = (, dis);
 }
}

Since the project needs to limit the range of movement of the camera, I added the restricted code at the end.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.