码迷,mamicode.com
首页 > 移动开发 > 详细

[Unity菜鸟] Character控制移动

时间:2014-07-01 22:04:58      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:blog   http   2014   art   问题   cti   

1. 给角色加角色控制器组件,然后用以下代码可以控制角色移动和跳跃

    float  speed  = 6.0f;
    float jumpSpeed  = 8.0f;
    float gravity  = 20.0f;

    private Vector3 moveDirection = Vector3.zero;

    void Start()
    {
       // gameObject.rigidbody = false;
    }
    void Update()
    {
        CharacterController controller = GetComponent<CharacterController>();

        if(controller.isGrounded)
        {
	        moveDirection =new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //Allows for player input
	        moveDirection = transform.TransformDirection(moveDirection); //How to move
	        moveDirection *= speed; //How fast to move
	
	        if(Input.GetButton("Jump"))
	        {
		        moveDirection.y = jumpSpeed;
	        }
        }
        //Apply gravity
        moveDirection.y -= gravity * Time.deltaTime;

        //Move the controller
        controller.Move(moveDirection * Time.deltaTime);	
	
    }

 

2. 添加角色控制器后人物下陷问题

bubuko.com,布布扣

[Unity菜鸟] Character控制移动,布布扣,bubuko.com

[Unity菜鸟] Character控制移动

标签:blog   http   2014   art   问题   cti   

原文地址:http://www.cnblogs.com/code1992/p/3816741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!