If you turn on the spot without moving the character doesnt step.接下来让我们来修改这两个小bug。将下面的代码添加到行走的if中的最顶部。animation["walk"].speed = 1; 这将保证行走动画在你向前走的时候执行向前的操作。下面在IsGrounded的if中的陈述放置在设置旋转的eulerAngles前面:// Create an animation cycle for when the character is turning on the spotif(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical")){ animation.CrossFade("walk");}if语句检测如果角色在没有向前行走时,进行旋转,在旋转的时候播放行走动画。现在可以比较完美的执行动画了。
下面需要让摄像机跟踪角色了。
粘贴代码
下面是完整的代码:private var walkSpeed : float = 1.0;private var gravity = 100.0;private var moveDirection : Vector3 = Vector3.zero;private var charController : CharacterController;function Start(){ charController = GetComponent(CharacterController); animation.wrapMode = WrapMode.Loop;}function Update () { if(charController.isGrounded == ***e) { if(Input.GetAxis("Vertical") > .1) { if(Input.GetButton("Run")) { animation.CrossFade("***n"); walkSpeed = 4; } else { animation["walk"].speed = 1; animation.CrossFade("walk"); walkSpeed = 1; } } else if(Input.GetAxis("Vertical") < -.1) { animation["walk"].speed = -1; animation.CrossFade("walk"); walkSpeed = 1; } else { animation.CrossFade("idle"); } // Create an animation cycle for when the character is turning on the spot if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical")) { animation.CrossFade("walk"); } transform.eulerAngles.y += Input.GetAxis("Horizontal"); // Calculate the movement direction (forward motion) moveDirection = Vector3(0,0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); } moveDirection.y -= gravity * Time.deltaTime; charController.Move(moveDirection * (Time.deltaTime * walkSpeed));