The pre-project realizes the role movement, now needs to complete the lens to follow and control.
First, the lens of the following
The idea is simple, first get the lens initial and character distance vector value, and then let the camera and the character to maintain the distance of the vector. The script is as follows:
NameSpace Followplayer
Private Transform player;
Private Vector3 offsetposition;
void Start ()
{
Player = Gameoject.findgameobjectwithtag ("Player"). Transform;
Offsetposition = transform.position-player.position;
}
Void Update ()
{
Transform.position = player.position + offsetposition;
}
This allows the lens to follow the character movement effect.
Second, the lens control
The control of the lens is decomposed into the lens stretching and rotating.
2.1 Stretching of the lens
The tensile nature of the lens is to change the vector difference between the characters, and after the script takes effect, the vector difference of the lens to the character is Offsetposition, and is a fixed value, so now it is necessary to change the fixed value according to the operation.
Use Input.getaxis ("Mouse Scrollwheel") to record this value.
The above script is updated to:
NameSpace Followplayer
public float scrollspeed = 5;
void Srollview ()
{
float distance = offsetposition.magnitude; But the vector variance of the offsetposition.
Distance + = Input.getaxis ("Mouse scrollwheel"); Change the vector variance value according to the middle mouse button
Offsetposition = offsetposition.normalized * distance; Change the vector to a standard vector and multiply the variance to the target distance
}
Called in void Upadate () is available.
UNITY3D-RPG Project Study notes (v)