unity-刚体角色控制器

刚体角色控制器

建议:最好创建的物体命名跟我一样
准备工作
创建一个空物体,重置一下位置,
把摄像机拖进空物体改名为“PlayerCamera”,把角色也拖进空物体改名为“playerObj”,
在空物体上添加“capsule collider碰撞器”以及刚体“rigidbody”
然后在将刚体里的锁定轴属性里的旋转的x,z锁定
就像这样:
unity-刚体角色控制器

PlayerRotate(脚本)

**效果:**人物视觉、人物随视觉旋转
步骤
1、
float y = Input.GetAxisRaw(“Mouse X”); 左右旋转
float x = Input.GetAxisRaw(“Mouse Y”); 上下旋转
2、
Vector3 rotate;
3、
rotate.y += y * rotateSpeed;
rotate.x -= x * rotateSpeed;
4、
mainCamera.rotation = Quaternion.Euler(rotate.x,rotate.y,0);
5、
rotate.x = Mathf.Clamp(rotate.x,-70,60);
限制的角度为-70,60
6、
player.rotation = Quaternion.Euler(0,rotate.y,0);
脚本挂载RigidBodyController物体上
完整的代码

    private Transform mainCamera;
	private Transform player;
    private void Start()
    {
        mainCamera = this.transform.Find("PlayerCamera");
    	player = this.transform;
    }
    Vector3 rotate;
    public float rotateSpeed = 2;
    private void Update()
    {
        float y = Input.GetAxisRaw("Mouse X");
        float x = Input.GetAxisRaw("Mouse Y");

        rotate.y += y * rotateSpeed;
        rotate.x -= x * rotateSpeed;

        rotate.x = Mathf.Clamp(rotate.x,-70,60);

        mainCamera.rotation = Quaternion.Euler(rotate.x,rotate.y,0);
        player.rotation = Quaternion.Euler(0,rotate.y,0);
    }

PlayerMove(脚本)

**效果:**人物移动以及人物跳跃
实现步骤
1、移动
float x = Input.GetAxisRaw(“Horizontal”);
float y = Input.GetAxisRaw(“Vertical”);
Vector3 dir = new Vector3(x * moveSpeed, 0, y * moveSpeed);
dir = tran.TransformDirection(dir);
dir *= moveSpeed;
orientation = dir - rig.velocity;
orientation.y = 0;
rig.AddForce(orientation, ForceMode.VelocityChange);
2、是否在地面
private void OnCollisionStay(Collision collisionInfo)
{
isGround = true;
}
private void OnCollisionExit(Collision collisionInfo)
{
isGround = false;
}
检测是否在地面,空中时物体不能移动
3、跳跃
if (Input.GetKeyDown(KeyCode.Space))
{
rig.velocity = new Vector3(orientation.x,Mathf.Sqrt(2 * gravity * jumpHeight),orientation.z);
}

  	private Rigidbody rig;
    private bool isGround;
    private Transform tran;
    public float moveSpeed = 5;
    public float gravity;
    public float jumpHeight;
    private void Start()
    { 
        rig = GetComponent<Rigidbody>();
        tran = this.transform;
        isGround = false;
    }

    private void FixedUpdate()
    {
        motor();
      
    }
    /// <summary>
    /// 物体的移动,跳跃行为,弊端耦合性太高需要封装
    /// </summary>
    private void motor()
    {
        Vector3 orientation;
        if (isGround)
        {
            float x = Input.GetAxisRaw("Horizontal");
            float y = Input.GetAxisRaw("Vertical");

            Vector3 dir = new Vector3(x * moveSpeed, 0, y * moveSpeed);
            dir = tran.TransformDirection(dir);
            dir *= moveSpeed;
            orientation = dir - rig.velocity;
            orientation.y = 0;
            rig.AddForce(orientation, ForceMode.VelocityChange);

            if (Input.GetKeyDown(KeyCode.Space))
            {
                rig.velocity = new Vector3(orientation.x,Mathf.Sqrt(2 * gravity * jumpHeight),orientation.z);
            }
        }

        rig.AddForce(new Vector3(0, -gravity * rig.mass,0));
    }

    private void OnCollisionStay(Collision collisionInfo)
    {
        isGround = true;
    }
    private void OnCollisionExit(Collision collisionInfo)
    {
        isGround = false;
    }

挂载在RigidBodyController下

上一篇:我的博皮


下一篇:LeetCode—189. 旋转数组(Rotate Array)——分析及代码(Java)