资源大湿 发表于 2012-11-25 14:19

第三人称character controller源码

本帖最后由 小米 于 2012-11-25 14:27 编辑

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 == true)
{
    if(Input.GetAxis("Vertical") > .1)
    {
      if(Input.GetButton("Run"))
      {
      animation.CrossFade("run");
      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));
}

qck123 发表于 2013-1-9 22:22

有源文件瓦

qck123 发表于 2013-1-9 22:23

有源文件瓦

shengbin88 发表于 2013-1-10 13:19

看起来真不错,下个看看

wtx412 发表于 2013-1-12 01:11

这个帖子不错,大家快来顶起来!

animan 发表于 2013-3-5 16:31

{:5_401:}{:5_401:}{:5_401:}

unity3D13 发表于 2013-3-15 11:33

角色控制入门经典,不错。

xirwajim 发表于 2013-5-3 11:56

{:5_397:}里面什么都没有啊?

jjww 发表于 2013-5-3 13:14

先从学习控制3D人物行走、动作开始,路漫漫,多谢楼主分享

YUEER1200 发表于 2013-5-18 21:19

真是好东西!适合新手学习!
页: [1] 2 3
查看完整版本: 第三人称character controller源码