> 文章列表 > unity角色控制器参数详解

unity角色控制器参数详解

unity角色控制器参数详解

场景:

unity角色控制器参数详解


方法

Slope Limit:角色控制器在斜坡上行走的最大角度。

Step Offset:角色控制器可以爬升的最大高度。

Skin Width:角色控制器的皮肤宽度,用于避免角色与其他物体之间的穿透。

Min Move Distance:角色控制器移动的最小距离。

Detect Collisions:是否检测碰撞。

Collision Flags:碰撞标志,用于检测碰撞。

Move Direction:角色控制器的移动方向。

Velocity:角色控制器的速度。

Is Grounded:角色控制器是否在地面上。

Material:角色控制器的物理材质。

unity角色控制器参数详解


举例子

现在利用角色控制器,wsad移动、空格跳跃脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerController : MonoBehaviour
{private CharacterController controller;private Vector3 moveDirection;public float speed = 5.0f;public float jumpSpeed = 8.0f;public float gravity = 20.0f;void Start(){controller = GetComponent<CharacterController>();}void Update(){if (controller.isGrounded){moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));moveDirection = transform.TransformDirection(moveDirection);moveDirection *= speed;if (Input.GetButton("Jump")){moveDirection.y = jumpSpeed;}}moveDirection.y -= gravity * Time.deltaTime;controller.Move(moveDirection * Time.deltaTime);}
}