大伟 类似魔兽世界角色控制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeiMove : MonoBehaviour
{
    private CharacterController controller;
    public float Speed = 10F;  //移动速度
    public float RotateSpeed = 1f; //旋转速度

    public float Gravity = -5f;  //自建重力加速度
    private Vector3 v3 = Vector3.zero;
    
    public Transform GroundCheck; //地面检测
    public float CheckRadius = 0.2f; //物理的检测半径
    private bool IsGround;
    public LayerMask layerMask;  //层标记

    public float JumpHeight = 3f; //跳跃高度

    void Start()
    {
        controller = transform.GetComponent<CharacterController>();
    }
    
    void  FixedUpdate ()
    {
        MoveLikeWow();        
    }
    private void MoveLikeWow()
    {
        IsGround = Physics.CheckSphere(GroundCheck.position,CheckRadius, layerMask);//检测与地面的碰撞
        if (IsGround && v3.y<0)
        {
            v3.y = 0;
        }

        if (IsGround && Input.GetButtonDown("Jump") )  //只有在地面上才能跳,不允许空中连跳
        {
            v3.y += Mathf.Sqrt(JumpHeight * -2 * Gravity);
        }


        var x = Input.GetAxis("Horizontal"); //水平ad控制方向旋转
        var z = Input.GetAxis("Vertical");   //垂直ws控制前进后退
        var move = transform.forward * Speed  * z * Time.deltaTime;
        controller.Move(move);
        transform.Rotate(Vector3.up,x*RotateSpeed); //绕Y轴旋转

        v3.y += Gravity * Time.deltaTime;     //重力加速度*持续时间=速度
        controller.Move(v3 * Time.deltaTime); //速度*时间=距离(x,z一直为0)

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值