In the previous Unity Learning--003: Role Controller (above) , saw the basic implementation of the point role control, mouse and keyboard control more rough implementation.
In this article, we mainly introduce the role control methods of several unity packages,
Then, it is expected to introduce several mainstream role control methods in the next chapter.
Words don't say much, open to engage:
is still the last few models dragged over
However, this time add the terrain, material, and sky box, natural light, as to how to add, please see here added terrain, etc.
Not black, the mood is also better, below to introduce several unity's own role control operations. (These methods are all unity's own APIs)
1. Charactercontroller.move () method
You need to add the Charactercontroller component to the role before using this method
Then add code scripts like the role
Velocity public float speed = 6.0F; High jump Speed public float jumpspeed = 8.0F; Gravity public float gravity = 20.0F; Mobile target private Vector3 movedirection = Vector3.zero; void Update () { //Get role Controller Charactercontroller controller = getcomponent<charactercontroller> (); Determine if the ground if (controller.isgrounded) { //Set move vector movedirection = new Vector3 (Input.getaxis (" Horizontal "), 0, Input.getaxis (" Vertical ")); Change the character toward movedirection = transform. Transformdirection (movedirection); Movedirection *= speed; Set Jumping if (Input.getbutton ("Jump")) movedirection.y = jumpspeed; } MOVEDIRECTION.Y-= Gravity * time.deltatime; Move the controller. Move (movedirection * time.deltatime); }
Here is changing the direction of the character before changing his position, it is clear that the direction of movement is not affected by character orientation, is the absolute position, the world coordinate system,
2. Charactercontroller.simple () method
public float speed = 3.0F; public float rotatespeed = 3.0F; void Update () { Charactercontroller controller = getcomponent<charactercontroller> (); The character turns to transform. Rotate (0, Input.getaxis ("horizontal") * rotatespeed, 0); Vector3 forward = transform. Transformdirection (Vector3.forward); float curspeed = speed * Input.getaxis ("Vertical"); Simply move the controller. Simplemove (Forward * curspeed); }
Here is the simple move, and the previous method is different, he is his own coordinate system, the direction changes with the character direction,
And it's not good to jump in here,
3, Rigidbody. Moveposition ()
Private Vector3 Speed =vector3.zero;void fixedupdate () {Speed = new Vector3 (Input.getaxis ("horizontal"), 0, Input.getaxis ("Vertical")); Rigidbody. Moveposition (rigidbody.position + speed *3* time.deltatime);}
The movement of a character through a rigid body, the rigid body and the role controller are different, the role controller contains a part of the rigid body properties, and is not affected by force
This section will constantly update the role control methods encountered, after the introduction of the camera will try to implement several mainstream methods of operation
Unity Learning--003: Role Controller (middle)