1 //CameraMovement.cs2 usingUnityengine;3 usingSystem.Collections;4 5 Public classCameramovement:monobehaviour6 {7 Public floatSmooth =1.5f;//Camera Tracking Speed8 9 Ten PrivateTransform player;//Reference Role Location One PrivateVector3 Relcamerapos;//the relative position of the camera and the character A Private floatRelcameraposmag;//distance vector length of camera to character - PrivateVector3 Newpos;//new location of the camera - the - voidAwake () - { - //Reference Role Location +Player =Gameobject.findgameobjectwithtag (Tags.player). Transform; - + //get the camera's position relative to the role ARelcamerapos = transform.position-player.position;//relative position = camera position-Role position atRelcameraposmag = Relcamerapos.magnitude-0.5f;//relative position vector length = relative position Length-0.5f prevents light from projecting collision ground - } - - - voidfixedupdate () - { in //Camera Initial Position = role position + role vs. Camera position -Vector3 Standardpos = player.position +Relcamerapos; to + //top position = role Position + character directly above * relative position vector length -Vector3 Abovepos = player.position + vector3.up *Relcameraposmag; the * //Create an array of length 5 to store 5 camera locations $Vector3[] checkpoints =Newvector3[5];Panax Notoginseng - //first detection camera standard position thecheckpoints[0] =Standardpos; + A //the three position interpolation between the three detection positions for the standard position to the top position is 25% 50% 75% thecheckpoints[1] = Vector3.lerp (Standardpos, Abovepos,0.25f); +checkpoints[2] = Vector3.lerp (Standardpos, Abovepos,0.5f); -checkpoints[3] = Vector3.lerp (Standardpos, Abovepos,0.75f); $ $ //final detection position for camera top position -checkpoints[4] =Abovepos; - the //checks if a role can be seen in each location by looping - for(inti =0; i < checkpoints.length; i++)Wuyi { the //If you can see the role - if(Viewingposcheck (Checkpoints[i])) Wu //Jump out of the loop - Break; About } $ - //smooth the camera position from the current position to the new position -Transform.position = Vector3.lerp (transform.position, newpos, smooth *time.deltatime); - A //Make sure the camera is facing the character direction + Smoothlookat (); the } - $ the BOOLViewingposcheck (Vector3 checkpos) the { the Raycasthit hit; the - //if Ray casts collide to an object in if(Physics.raycast (Checkpos, Player.position-checkpos, outHit , relcameraposmag)) the //if the ray-casting collision point is not a role position the if(Hit.transform! =player) About //current detection position not appropriate return false the return false; the the //if we haven ' t hit anything or we have struck the player, this is an appropriate position. If ray casting does not collide to anything or the collision point is a role position, update the current Detection position for the camera's new position +Newpos =Checkpos; - return true; the }Bayi the the voidSmoothlookat () - { - //create a vector from a camera to a character theVector3 relplayerposition = player.position-transform.position; the the //Create a rotation angle based on the camera-to-character Vector thequaternion lookatrotation =quaternion.lookrotation (relplayerposition, vector3.up); - the //let the camera go from the current angle to the angle of rotation created theTransform.rotation = Quaternion.lerp (transform.rotation, lookatrotation, smooth *time.deltatime); the }94}
The realization of the role of the camera in the following game