新输入系统用代码绑定动作

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

public class PlayerControllerWithCode : MonoBehaviour
{
    private CharacterController controller;
    [SerializeField] private float moveSpeed = 15f;
    [SerializeField] private float mouseSensitivity = 60f;
    [SerializeField] private Camera playerEyes;
    private float eyesAngleLimit;//眼睛旋转的限制范围
    private Vector3 jumpVector;//跳跃向量
    [SerializeField] private float jumpHeight = 4f;//可修改
    private float gravity = -9.8f;
    private bool isOnGround = true;//判断玩家是否在地面上
    //图层
    [SerializeField] private GameObject groundCheck;
    [SerializeField] public LayerMask layerMask;
    //private MyInputActions inputActions;
    private InputAction moveAction;
    private InputAction jumpAction;
    //private InputAction lookAction;
    private void Start()
    {
        controller = GetComponent<CharacterController>();//获取组件
        moveAction = new InputAction("Move", binding:"<Gamepad>/LeftStick") ;//绑定游戏手柄的左摇杆
        moveAction.AddCompositeBinding("Dpad")//组合绑定
        .With ("Up","<keyboard>/w")
        .With("Up", "<keyboard>/upArrow")
        .With("Down", "<keyboard>/s")
        .With("Down", "<keyboard>/dowmArrow")
        .With("Left", "<keyboard>/a")
        .With("Left", "<keyboard>/leftArrow")
        .With("Right", "<keyboard>/d")
        .With("Right", "<keyboard>/rightArrow");
        moveAction.Enable();

        //lookAction = new InputAction("Look");

        jumpAction = new InputAction("Jump",binding :"<Gamepad >/a");
        jumpAction.AddBinding("<keyboard>/space");
        jumpAction.Enable();
    }

    private void Update()
    {
        //移动
        float horizontalInput = moveAction .ReadValue<Vector2>().x;
        float verticalInput = moveAction .ReadValue<Vector2>().y;
        Vector3 moveDirection = transform.forward * verticalInput + transform.right * horizontalInput;
        controller.Move(moveDirection.normalized * moveSpeed * Time.deltaTime );
        //normalized保留方向,使长度归一,否则长度也会影响速度

        //    //身体在水平方向旋转
        float mouseX = Mouse.current.delta.ReadValue().x;
        transform.Rotate(Vector3.up, mouseX * mouseSensitivity * Time.deltaTime);//省略了this
        //    //眼睛在竖直方向上旋转
        float mouseY = Mouse.current.delta.ReadValue().y;
        //transform.Rotate(Vector3.up, mouseY * mouseSensitivity * Time.deltaTime);

        //    //以下代码不能合适地控制旋转的角度
        eyesAngleLimit -= mouseY * mouseSensitivity * Time.deltaTime;
        eyesAngleLimit = Mathf.Clamp(eyesAngleLimit, -80f, 80f);
         playerEyes.transform.localRotation  = Quaternion.Euler(eyesAngleLimit, 0, 0);
        isOnGround = Physics.CheckSphere(groundCheck.transform.position, 0.3f, layerMask);//中心点,半径,检测图层

        if (jumpAction .triggered && isOnGround)
        {
            jumpVector.y = jumpHeight;

        }
        else
        {
            jumpVector.y += gravity * Time.deltaTime;//模仿有重力的下落
        }
        controller.Move(jumpVector * Time.deltaTime);//传到Move()方法中才能移动
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值