角色控制器wasd移动

Posted Jessica巨人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了角色控制器wasd移动相关的知识,希望对你有一定的参考价值。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * 1.wasd控制胶囊体移动
 * 2.鼠标左右滑动左右旋转
 * 3.鼠标上下移动,相机上下转动  限制转动的角度
 * 4.鼠标的显示和锁定
 * 5.跑步模式,设置input manager, Run-left shift
 */

public class DoctorController : MonoBehaviour

    private CharacterController characterController;
    public float moveSpeed;

    public float mouseSensitivity;
    private float angleY;

    private Transform cameraTran;
    private float angleX;

    public float runningSpeed;
    void Start()
    
        characterController = GetComponent<CharacterController>();
        moveSpeed = 5;

        mouseSensitivity = 2.4F;
        angleY=transform.eulerAngles.y;

        cameraTran = Camera.main.transform;
        angleX = cameraTran.eulerAngles.x;

        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

        runningSpeed = 7;
    
   
    void Update()
    
        Move();
        Turn();
        Look();
    

    // 1.wasd控制胶囊体移动
    public void Move()
    
        //三元运算符
        float actualSpeed = Input.GetButton("Run") ? runningSpeed : moveSpeed;

        Vector3 move = Vector3.zero;
        move = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
        move.Normalize();
        //move = move * moveSpeed * Time.deltaTime;
        move = move * actualSpeed * Time.deltaTime;
        move = transform.TransformDirection(move);
        characterController.Move(move);

     
    

    //2.鼠标左右滑动左右旋转
    private void Turn()
    
        float turenAngle = Input.GetAxis("Mouse X") * mouseSensitivity;
        angleY = angleY + turenAngle;
        transform.eulerAngles = new Vector3(transform.eulerAngles.x,angleY,transform.eulerAngles.z);
    
    //3.鼠标上下移动,相机上下转动  限制转动的角度
    private void Look()
    
        float LooknAngle = -Input.GetAxis("Mouse Y") * mouseSensitivity;
        //angleX = angleX + LooknAngle;
        angleX=Mathf.Clamp(angleX+LooknAngle,-60,60);
        cameraTran.eulerAngles = new Vector3(angleX,cameraTran.eulerAngles.y,cameraTran.eulerAngles.z);
    


以上是关于角色控制器wasd移动的主要内容,如果未能解决你的问题,请参考以下文章

unity---小球击打围墙

尝试使用箭头键和 WASD 同时移动两个对象

胶囊对撞机不统一跳跃

unity3d 第三人称视角的人物移动以及相机控制

Infinite V3 Release Note

Unity3D鼠标控制摄像机“左右移动控制视角+WASD键盘控制前后左右+空格键抬升高度”脚本