Focus: The operation of the vector. After the speed is obtained horizontally and vertically, the direction is reset to synchronize the direction with the field of view (i.e., the camera is in the same direction as the character)
Here's an example of how to do this:
1. Create a terrain (terrain), two cube (cube) (reference), capsule (Capsule). Place the main camera under the capsule as a sub-object and reset the position information.
(for easy observation you can create several material balls to attach to the object)
2. Move the camera to the game view to achieve the following:
3. Create two scripts one to control movement another control field of rotation: (I created the move and Freelook two scripts)
Move script content:
Using unityengine;using System.collections;public class Move:monobehaviour {public gameobject camer;//camera/ Initializationvoid Start () {}//Update is called once per framevoid update () {Vector3 forward = Camer.transform.Transfor Mdirection (Vector3.forward);//Record camera forward to Vector3 right=camer.transform.transformdirection (vector3.right);// Record the right direction of the camera float H = Input.getaxis ("horizontal"); float V = Input.getaxis ("Vertical"); Vector3 he = H * right + V * forward;//The speed is synthesized getcomponent<rigidbody> (). Moveposition (transform.position+ He * 5 * time.deltatime);//Control Move}}
Freelook Script content:
Using unityengine;using System.collections;public class Freelook:monobehaviour {public Gameobject camer;private float s peed=5.0f;//rpm//use of this for Initializationvoid Start () {}//Update is called once per framevoid update () {CAMER.TRANSF Orm. Rotatearound (This.transform.position,vector3.up,speed*input.getaxis ("Mouse X"));//camera with a character-centric, own y-axis rotation}}
4. Assign the camera to the script:
Third-person character movement and free-moving field of view (Rigidbody implementation)