添加角色控制组件
属性
center | 自身的位置 |
height | 自身的高度 |
isGrounded | 判断自身是否位于地面上 |
方法
Move | 按照长度进行移动,会模拟重力直接掉到地面上 |
SimpleMove | 按照向量进行移动,不模拟重力不会掉到地面上,必须*Time.deltaTime |
消息事件
OnControllerColliderHit | 移动时碰到别的碰撞器时触发,会一直触发 |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCC : MonoBehaviour {
public float speed = 3;
private CharacterController cc;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();//获取角色控制组件
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");//左右
float v = Input.GetAxis("Vertical");//上下
//按照长度进行移动,会模拟重力直接掉到地面上
cc.SimpleMove(new Vector3(h, 0, v) * speed);
//按照向量进行移动,不模拟重力不会掉到地面上,必须*Time.deltaTime
cc.Move(new Vector3(h, 0, v) * speed * Time.deltaTime);
Debug.Log(cc.isGrounded);//角色是否在地面上
}
//移动时碰到别的碰撞器时触发,会一直触发
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log(hit.collider);//输出碰撞到的对象
}
}