纳金网
标题: character controller的一个使用例子 [打印本页]
作者: 狂风大尉 时间: 2014-9-28 22:58
标题: character controller的一个使用例子
using UnityEngine;
using System.Collections;
public class man_control : MonoBehaviour {
// Use this for initialization
public float speed=1.0f;
public float jumpSpeed=1.0F;
public float gravity=5.0F;
float A_Rotate_Speed=50.0f;
private Vector3 moveDirection=Vector3.zero;
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController controller=GetComponent<CharacterController>();
if(controller.isGrounded)
{
moveDirection=new Vector3(0,0,Input.GetAxis("Horizontal"));
moveDirection=transform.TransformDirection(moveDirection);
moveDirection*=speed;
if(Input.GetKey(KeyCode.Space))
{
moveDirection.y=jumpSpeed;
}
}
moveDirection.y-=gravity*Time.deltaTime;
controller.Move(moveDirection*Time.deltaTime);
if(Input.GetKeyDown(KeyCode.Q))
{
transform.Rotate(new Vector3(0,90,0),Space.Self);
}
if(Input.GetKeyDown(KeyCode.E))
{
transform.Rotate(new Vector3(0,-90,0),Space.Self);
}
}
}
//除了这个之外,charactercontroller默认自带有刚体,使用Move函数不会启用重力,需要同上自己写。使用SimpleMove函数会自动启用重力。
//另外,一个charactercontroller需要和一个自带有rigidbody组件的物体才能产生碰撞。
作者: hyui 时间: 2014-10-2 07:03
Good to learn !
作者: oelongeo 时间: 2014-10-4 16:08
感谢分享与指导
-- 不知道是否有手机移动操纵杆相关的代码与指导? ( 好多不明白)
作者: Kadina 时间: 2014-10-15 08:50
Good to learn !
作者: rongctor 时间: 2014-10-15 11:00
不错,学习中。
欢迎光临 纳金网 (http://go.narkii.com/club/) |
Powered by Discuz! X2.5 |