1\ How to control the movement of an object via input
Premise: Object must add rigidbody (rigid body) component
1. Initializing components
Select Rigidbody RB = Getcomponent<rigidbody> () in the Awake () or Start () method;
2. Get input
Write get code in void Update () method
private float M_inputverticalvalue; private float M_inputhorizontalvalue; void Update () {M_inputverticalvalue = Input.getaxis ( " vertical " ); // input.getaxis (String AxisName), The default forward is 1, the negative direction is-1, the fixed is 0 m_inputhorizontalvalue = Input.getaxis ( " horizontal );}
3. Use the resulting input value in update () or fixedupdate () m_ Inputverticalvalue, m_inputhorizontalvalue;
The first type of rigidbody.addforce () private float force = 10;void Update () { ... Rb. Addforce (force*m_inputverticalvalue*time.deltatime);} Another type of rigidbody.moveposition () rigidbody.moverotation ();p rivate void Fixedupdate () { //Move and turn the tank . Move (); Turn (); } private void Move () { //Adjust the position of the tank based on the player ' s input. Vector3 movement = transform.forward*m_speed*m_movementinputvalue*time.deltatime;m_rigidbody.moveposition ( Transform.position + movement); } private void Turn () {float Turn = m_turnspeed * M_turninputvalue * time.deltatime; Adjust the rotation of the tank based on the player ' s input. quaternion turnquaternion = Quaternion.euler (0f,turn,0f); M_rigidbody.moverotation (Transform.rotation * turnquaternion); }
2\camera How to adjust position and field of view as the tank moves? Position Zoom
Solution: Calculate the average position by the three-dimensional position of each tank
The field of view is obtained by finding the distance from the average position of each tank.
UsingUnityengine;PublicClasscameracontrol:monobehaviour{Publicfloat M_damptime =0.2f;Publicfloat M_screenedgebuffer =4f;Publicfloat m_minsize =6.5f;PublicTransform[] M_targets;PrivateCamera M_camera;PrivateFloatM_zoomspeed;PrivateVector3 m_movevelocity;PrivateVector3 m_desiredposition;PrivatevoidAwake () {M_camera = getcomponentinchildren<camera>(); }PrivatevoidFixedupdate () {Move (); Zoom (); }PrivatevoidMove () {findaverageposition (); transform.position = Vector3.smoothdamp (transform.position, M_desiredposition,RefM_movevelocity, M_damptime); }PrivatevoidFindaverageposition () {Vector3 Averagepos =NewVector3 ();int numtargets =0;for (int i =0; i < m_targets.length; i++) {if (!M_targets[i].gameobject.activeself)Continue; Averagepos + =M_targets[i].position; numtargets++; }if (Numtargets >0) Averagepos/=Numtargets; Averagepos.y =TRANSFORM.POSITION.Y; M_desiredposition =Averagepos; }PrivatevoidZoom () {float requiredsize =Findrequiredsize (); M_camera.orthographicsize = Mathf.smoothdamp (M_camera.orthographicsize, Requiredsize,RefM_zoomspeed, M_damptime);//Smoothdamp is a gradual process.//Requires ref Zoomspeed and m_damptime to calculate the gradient process}PrivateFloatFindrequiredsize () {Vector3 Desiredlocalpos =Transform. Inversetransformpoint (m_desiredposition);Float size =0f;for (int i =0; i < m_targets.length; i++) {if (!M_targets[i].gameobject.activeself)Continue; Vector3 Targetlocalpos =Transform. Inversetransformpoint (m_targets[i].position);//With the current field of view as the reference system, 3-dimensional to 2-dimensional Vector3 desiredpostotarget = Targetlocalpos-Desiredlocalpos;//Calculates the relative position of size =Mathf.max (Size, Mathf.abs (DESIREDPOSTOTARGET.Y));// take the maximum y-direction distance size = Mathf.max (size, Mathf.abs ( desiredpostotarget.x)/ M_camera.aspect); // Take the maximum resolution-compliant distance "size + = M_screenedgebuffer; size = Mathf.max (size, m_minsize) ; return size;} public void setstartpositionandsize () // Adjust the position and field of view directly without the gradient process {findaverageposition (); Transform.position = m_desiredposition; m_camera.orthographicsize = Findrequiredsize (); }}
Unity3d notes-tanks Tutorial Knowledge points Summary